Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jens T · Jul 21, 2010 at 02:36 PM · errorobject-reference-error

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

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mike 3 · Jul 21, 2010 at 03:11 PM 0
Share

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

avatar image Cyclops · Jul 21, 2010 at 04:03 PM 0
Share

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. :)

avatar image Cyclops · Jul 21, 2010 at 04:05 PM 1
Share

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.

avatar image Mike 3 · Jul 21, 2010 at 04:39 PM 1
Share

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

avatar image Cyclops · Jul 21, 2010 at 05:33 PM 0
Share

@$$anonymous$$ike, 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. :)

Show more comments

6 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by SteveTwo · Jul 30, 2010 at 11:22 AM

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LijuDeveloper · Nov 18, 2013 at 10:31 AM 0
Share

In my project i declare a variable like

       public static Camera DailyBoxBonusCamera = GameObject.Find ("Daily Bonus Box $$anonymous$$ain Camera").camera ;

After Removing "GameObject.Find ()" , problem was solved

avatar image JoshMBeyer · Jan 23, 2015 at 01:30 PM 0
Share

I also had to remove "GameObject.Find()"

avatar image
3

Answer by neoRiley · May 08, 2012 at 03:54 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by Joshua Beeler · Aug 22, 2013 at 07:34 PM

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?)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Valentin_B · Feb 03, 2014 at 11:04 AM 0
Share

thank you very much, this was the answer I was looking for and I could never have guessed it by myself, many thanks.

avatar image
0

Answer by Josef.du · Mar 23, 2011 at 07:56 AM

I can conform the Answer of SteveTwo.

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

Thanks!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Shinda · Mar 14, 2014 at 03:28 PM

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! ^.^

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UnityException: Input Axis Rotate2 is not setup. 0 Answers

Object reference not set to an instance of an object 1 Answer

How do I fix this error 0 Answers

Object reference not set to an instance of an object in c# class. 1 Answer

Object reference not set to an instance of an object... again 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges