x


Error message : You are not allowed to call this function when declaring a variable.

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)

class Constraint{

    private var partRef:Transform;

    function Constraint(constraintName:String){
        setPartRef();
    }

    function setPartRef(){
        this.partRef = GameObject.Find("box").transform;
    }
}

Cant find a logical solution to this. I am using Javascript, not C#. I tried the Awake() function ,but that does not work. Thank you

more ▼

asked Jul 21 '10 at 02:36 PM

Jens T gravatar image

Jens T
138 6 7 17

There isn't anything wrong with the above code (Besides the layout, but bleh). You'll need to paste in a non simplified version

Jul 21 '10 at 03:11 PM Mike 3

Where are you creating an instance of the class Constraint? Is it possibly being created before any GameObjects exist, thereby making GameObject.Find() unavailable? I'm not that familiar with Unity's execution flow, so not sure what happens when. :)

Jul 21 '10 at 04:03 PM Cyclops

Note - if so, then you would need to remove the SetPartRef() call from the constructor function, and call it from outside, after the object is fully created.

Jul 21 '10 at 04:05 PM Cyclops

Also - is there any reason why you need to use gameobject.find from setPartRef instead of passing in a transform reference (into the constructor or the function) ?

Jul 21 '10 at 04:39 PM Mike 3

@Mike, based on the variable constraintName:String, I presumed he was going to replace box with that String variable - so he doesn't necessarily have a reference at compile-time. I could be wrong. :)

Jul 21 '10 at 05:33 PM Cyclops
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Jul 30 '10 at 11:22 AM

SteveTwo gravatar image

SteveTwo
91 1 1 7

(comments are locked)
10|3000 characters needed characters left

I can conform the Answer of SteveTwo.

Backing up, deleting, recreating and pasting was the sollution for me as well.

Thanks!

more ▼

answered Mar 23 '11 at 07:56 AM

Josef gravatar image

Josef
1 2

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered May 08 '12 at 03:54 PM

neoRiley gravatar image

neoRiley
126 3 4 10

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1951
x9
x6

asked: Jul 21 '10 at 02:36 PM

Seen: 3999 times

Last Updated: May 08 '12 at 03:54 PM