Yesterday I updated unity from unity5 to 2018.2.2f1. Unity scripts are not loading after Update 2018.2.2f1.
Once I try to play the Scene the scripts are not loaded and I can't add the script again it gives this error:
Can't add script component 'CubeScript' because the script class
cannot be found. Make sure that there are no compile errors and that
the file name and class name match.
If you still have the old copy of the project, upgrade the Unity project to Unity 2017 first then to 2018.2.2f1.
Here are the few possible reasons you may get this error(Ordered from very likely)
1.Script name does not match class name.
If script name is called MyClass, the class name must be MyClass. This is also case-sensitive. Double check to make sure that this is not the issue. To make sure that's not the issue, copy the class name and paste it as the script name to make sure that this is not the issue.
Note that if you have have multiple classes in one script, the class name that should match with the script name is the class that derives from MonoBehaviour.
2.There is an error in your script. Since this is an upgrade, there is a chance you're using an API that is now deprecated and removed. Open your script with Visual Studio and see if there is an error there then fix it. There is usually a red line under a code that indicates there is an error.
3.Bad import with the Unity importer and its automatic upgrade script.
Things to try:
A.The first thing to do is restart the Unity Editor.
B.Right click on the Project Tab then click "Reimport All"
C.If there is still issue, the only left is deleting the problematic script and creating a new one. There is an easier way to do this if the script is attached to many GameObjects in your scene.
A.Open the script, copy its content into notepad.
B.From the Editor and on the Project tab right click on the script "CubeScript", select "Find References In Scene".
C.Unity will now only show all the GameObjects that has this script attached to them. Delete the old script. Create a new one then copy the content from the notepad to this new script. Now, you can just drag the new script to all the filtered GameObject in the scene. Do this for every script effected. This is a manual work but should fix your issues when completed.
Here, is some reason why that's heaping.
1. Your Script Name And your class name isn't same.
2. May be there is an error in your script.
Here, is the few way to resolve it.
1. Make a same name of script and class in to this script.
2. find there is the error? and try to resolve it.
Posting here for historic purposes and if someone from Google ends up here, I strictly had the same problem (I'm on Windows 10) and here's how I fixed it :
If you click on the Console, you should see a blank error. Don't worry, it's a Editor bug, and you can see the Editor Log by selecting the console's window dropdown and selecting Open Editor Log.
For me, the problem was that "Tools\Roslyn\csc" could not be found, something that Unity uses to compile C# files.
I opened C:\Program Files\Unity\Hub\Editor\2019.2.14f1\Editor\Data\Tools\RoslynScripts\unity_csc.bat and I added ".exe" to the line "%APPLICATION_CONTENTS%\Tools\Roslyn\csc" /shared %*
(This line should now be : "%APPLICATION_CONTENTS%\Tools\Roslyn\csc.exe" /shared %*)
Now it works perfectly.
I'm completely new to Unity and game development, but I found that making the error true by entering an incorrect name, then entering the right one seems to "trick" Unity into fixing its error.
I had the same error show up but for different reasons (in case someone ends up here from google).
I was a beginner and named my file with a space in it. I got the same error after that. Make sure to NOT include spaces in your C# script.
One more thing which might help apart from other answers
Make the class which inherits from the MonoBehaviour the first class in the file. It fixed this issue for me.
Checking the console bugs and fix them is a good way to solve the problems "Can't add script component because the script class cannot be found?", although the bug seems to be irrelevant.
This works for me when I fix a bug in the other script.
for me I found out it was a script that I wasn't using which was linked to the player and other scripts that I deleted.
For me, the problem was that there was an error in another script.
Once I fixed the error, I could FINALLY attach my other scripts onto my gameobjects.
I just had this issue where I had a perfect script, no errors, and the same name as it should be but no matter what it wouldn't let me add it to a gameObject.
There was another script that was completely unrelated but it had an error in that and somehow that prevented me from adding my good new script.
Like some folks already posted here - the issue might be of a different code.
I was trying to add a script that was referenced by another one which had errors - it doesn't tell you that is the reason, although visual studio kinda suggests it by showing you the errors in the open script.
Just delete the bigger (errored) script and start adding to the unity project from the basic ones.
I had this issue where I had a good script, no errors, good name, restarting unity, following a tutorial exactly but still had this issue.
The way I fixed this is just deleting this buggy script file and creating it again. It fixed my problem perfectly! Now I could add the script to my game object with no such errors!
Hope this helps you! Cheers!
Related
I am pretty new to Unity and Vuforia. I am trying to do solve this following problem. I have a script file that I am using for 2 different Image targets. Only 1 script should be running at a time. So i opened "DefaultObserverEventHandler.cs" to handle tracking events. I want to disable the script on that particular Image target when the tracking is lost. But when i try to get a reference on the script "QuizzBehaviour" i get an error. "QuizzBehaviour" is located in my Assets folder. Thanks in advance.
Ok, I fixed it. I made my own "DefaultObserverEventHandler.cs" script and placed it in the same folder where my "QuizzBehaviour.cs" script is.
"Find" will search through objects in scene not in your folders.However, at all costs you should avoid searching for objects by typing their name manually.In your case, the matter is quite simple. You can either add a script to an object and then use the get component command, or you can make an abstract class and refer to it.
i currently try to extend the unity editor with custom EditorWindow implementations. It follows loosely this guide.
I try to save a asset containing serialized objects as a sort of database.
That works just fine!
However. If i close unity and reopen it
db = (FigureDB)AssetDatabase.LoadAssetAtPath("Assets/Logic/Database/FigureDB.asset", typeof(FigureDB));
doesnt load the asset file unless i manually reimport the EditorWindow implementation within unity (rightclick on cs-file -> reimport).
I code in Visual Studio (whether this matters or not.... i suspected a line-endings-problem, but it doesnt seem to be the case)
After reopening Unity i also see within the inspector (while selecting the asset) The associated script can not be loaded. Please fix any compile errors and assign a valid script (even though there are no compile time errors visible)
Any suggestions? If more info is necessary i will gladly provide them, however it doesnt seem to be a code problem but rather an IDE-problem.
Do you had use this?
AssetDatabase.ImportAsset("Assets/Logic/Database/FigureDB.asset", ImportAssetOptions.Default);
For a schoolproject we're working on a project for loading BIM-models into unity. We are working with .ifc files, which we convert to .dae for the time being. We created a script for importing the files using EditorUtility.OpenFilePanel. We are using IFCOpenshell's IFCConvert (a command line utility).
This goes well, and the Asset shows up in the Assets folder. The only thing left to do now, is importing the asset runtime into the Hierarchy *without dragging it into the scene or hierarchy *.
We've tried to use a WWW stream to get the file and assign AssetBundles to it and a few other methods, but no luck yet.
"The only thing left to do now, is importing the asset runtime into the Hierarchy"
Unless I misunderstand you: for items in "Project" (ie, your assets), if you want to put them in the current scene .. drag them in to the scene.
(ie, drag them either in to the Heirarchy panel or just literally in to the middle of the scene Editor.)
Are you saying you're trying to automate this ?? (I don't really see how you could - any button you added would be more difficult than .. just dragging them in.)
If you are simply asking how to load (at runtime) Resources, just look in to
Resources.LoadAll()
You'll learn that you have to keep the items in a "Resources/" folder and so on. It is well documented. There are 1000s of QA on the topic. Example code fragment
ra = Resources.LoadAll("VO/", typeof(AudioClip) ).Cast<AudioClip>().ToArray();
It is beyond the scope of a QA site to provide a tutorial on "Resources" but you can instantly find this on Unity3d.com.
Note that if, very simply, you just want to use a particular model in the build. Do nothing more than, drag it to the scene and place it where you want. Then in code just
public GameObject yourBuilding;
and use inspector-dragging to link that variable as you wish. Then you can use that variable as you wish. Note that for large complicated models, often you will go ahead and just have different inspector variables for the different parts, rather than even bothering to drill-down. (So, you might have "hospital", "eastWing" "corridor" .. etc. and just go ahead and "inspector-drag" those.)
In the past I've accessed variables from other java scripts by doing
static var somevar;
In one script. And in the other:
OtherScript.somevar;
But I need to access a variable from C# code in a js script. I've tried:
Public static bool somevar;
In the C# file and in the js file:
OtherScript.somevar;
But I get the error: Unknown identifier 'somevar' in the js file
Just put your Unityscript file in the Plugin or in the Standard Assets folders.
Explanation:
In the "standard" way, you can only access a Unityscript variable from a C# script, but you cannot do the opposite. That's just C# code is compiled before Unityscript's one.
So, in order to achieve what you are trying to do, you must move your Unityscript file into a "special" folder (in Unity, they are the Plugin and the Standard Assets ones), so that it would be compiled at first.
After that, you should achieve the access with the usual logic.
You can't call a Static Object from JS and call its properties. You would have to get help from Unity GameObject to get the Object as Component as everything is an ObjectComponent in Unity. Then use that Component(Technically the Script) as your reference.
Example
GameObject name is "MyCsharpGameObject" <- the GameObject that has the Script
Then use.
theCscharpscript : OtherScript;
theCsharpscript = GameObject.find("MyCsharpGameObject").GetComponent("OtherScript");
On usage from the JS
theCsharpscript.thevariable;
Remember when using JS in MonoDevelop it doesn't give you Intellisence and that is just Normal. You can't however access a Static "Class" that is not attached to a GameObject.
if you insist on doing so. Then check Andrea's Answer. But his explanation might be complicated for you to work on.
But summing up the point that he was saying is. C# works in a NameSpace and JS is not part of that NameSpace so at lease you will have to somewhat include it in, and NameSpace is nothing more than a Folder Location in an Project. Simple as that.
However you must always remember to use PragmaStrict in this as it will always be transformed into a MonoDevelop calls even though you did not Extend from it.
Hope this help you.
So I'm trying to implement soundeffects whenever I click on an item. I've added a .wav sound to my Content folder.
Then I typed the following code in the Game1 class constructor:
SoundEffect soundEffect;
After that I wrote the following code in the Initialize function:
soundEffect = Content.Load<SoundEffect>("Laser_Shoot25.xnb");
Finally, I wrote the following code in my clickFunction:
soundEffect.Play();
When I tried running it, it gave me the following error.
Could not load nameOfSoundFile asset.
I've looked on Google to see if someone else had this same problem, but none of those solutions seem to work for me.
Does anyone know what else I'm supposed to do to make this work?
First thing that comes to mind is the possibility of you loading your assets before the Content object gets initialized. Please post the relevant code where you load your asset to know if this is the case (usually the initialize and LoadContent methods of your Game1 class).
The other possibility is the asset file format.
I'm not up to date with Monogame as I stopped using it with version 3.2, but if things remain the same on this aspect; then for working with sounds and spritefonts you need to add the asset as an exported xnb file, created by using the actual XNA content pipeline.
I have a blog post that explains exactly how to do all of this, which can be found here.
First, you need to install XNA on your machine (in the same post, it explains how to do it if you have Windows 8 and/or Visual Studio 2012/2013).
Then, just follow the steps detailed on the post:
So, once we have this, let’s create a new Windows Game project (it has
to be a game and not only a Content project as we will need to access
the game output directory).
After having this done, we can add our sound effects/instances to the
Content project by right clicking, selecting “Add existing item” and
choosing it in the folder explorer.
Before continuing, make sure you set the properties of the sound as
desired (in my case, I’ll be using a SoundEffect so I need to specify
it in the Content Processor field):
All that remains is to add the xnb file to the Content folder of your
Monogame project.