I have created a slider that moves a box back and forth, and the Unity applications WORKS. But i still get this errocode.
UnityException: You are not allowed to call this function when declaring a variable.
Move it to the line after without a variable declaration.
If you are using C# don't use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function.
This is the class (simplified)
private var partRef:Transform;
this.partRef = GameObject.Find("box").transform;
I have run into this mysterious message a couple time and have tracked it down to a bugged .js file. It seems to be rare and random.
If there is nothing wrong with the code and everything seems fine:
- Backup the contents of the script
- Delete the script
- Recreate the script and past back the contents
The next time you run your project you shouldn't recieve the error.
Clearing the contents of the script and re-pasting it won't work. The file needs to be deleted and remade.
In my situation, it was a legitimate error that I was able to fix - c# project btw
I had both a ScriptableObject and MonoBehaviour with [ExecuteInEditMode]. The ScriptableObject had a property I was declaring AND initializing at the same time that relied on the MonoBehaviours singleton instance to be set:
public SystemLanguage language = LocalizationManager.Instance.currentLanguage;
It was calling for a singleton instance on the MonoBehaviour object at design time before Awake or Start had been called. The Instance getter/setter of the singleton had to look for a GameObject in the scene if the instance hadn’t been set yet and bam, there was the problem.
All I had to do was remove the initialization of the property to a point that was AFTER the Awake() commands had run through the scene.
The problem is you’re trying to invoke the Unity API from a thread other than the main thread, which is not allowed.
Constructor calls and field initialization for MonoBehaviours occur on the loading thread. Your Constraint class constructor is called, which calls setPartRef(), which calls GameObject.Find() - there’s the Unity API on the loading thread.
If your Constraint is intended to be a MonoBehaviour, move the call to setPartRef() from the constructor to the Start() function. If it’s not intended to be a MonoBehaviour, make sure it gets new’d from within the Awake() or Start() of a MonoBehaviour, not within a MonoBehaviour’s field initialization. (Hint: do you have a MonoBehaviour that has a field of type Constraint?)
I can conform the Answer of SteveTwo.
Backing up, deleting, recreating and pasting was the sollution for me as well.
Or comment the script out (STRG+ALT+C). Save it. Back to Unity, than back to MonoDevelop.
uncomment the script (STRG+ALT+C). Save again–>>Error gone! ^.^
You guys are tricking the compiler. All you have to do is follow the directions of the error. For example instead of writing:
public var cat = GameObject.Find("cat-model");
You should write:
public var cat;
//Then on the next line write:
cat = GameObject.Find("cat=model");
I don’t know why Monodevelop/Unity wants them on seperate lines, but it requires you to declare the variable on one line and assign a value on another line.