• 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 Aldwoni_legacy · Jun 28, 2011 at 08:05 AM · nullreferenceexceptionboolean

NullReferenceException with boolean from other script

I have 2 scripts.

script 1:

 var waarde : boolean;
 
 function Awake(){
     waarde = false;
     }

script 2:

 var waarde:boolean;
 var script1;
 
 function Awake(){
 script1 = this.gameObject.GetComponent(script1);
 waarde = script1.waarde; <-- NullReference?
 }
Comment
Add comment
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

2 Replies

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

Answer by GuyTidhar · Jun 28, 2011 at 08:10 AM

You can not know which of the objects will be initialize first. For this reason, you should wait a frame before looking for other objects.

Do this in script 2:

  function Awake()
  {
    StartCoroutine(Initiate());
  }

  private function Initiate()
  {
     yield;
     script1 = this.gameObject.GetComponent(script1);
     waarde = script1.waarde;
  }
Comment
Add comment · Show 5 · 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 Aldwoni_legacy · Jun 28, 2011 at 08:18 AM 0
Share

So I should do this in every script? I got this warning? BCE0043: Unexpected token: IEnumerator.

avatar image GuyTidhar · Jun 28, 2011 at 08:23 AM 0
Share

Oops, sorry for mixing this up - I work with C# too.

Ins$$anonymous$$d of " private IEnumerator Initiate()" do:

"private function Initiate()"

And you generally would be doing a good thing if you'd use this to avoid null reference on the get go.

I think Unity guys are going to add some management for us to be able to set the initialize order of scripts in Unity 3.5. But until then its a good idea.

avatar image Aldwoni_legacy · Jun 28, 2011 at 08:33 AM 0
Share

I still get the NullRefenrenceException:NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cache$$anonymous$$eyName, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name) UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name)

avatar image GuyTidhar · Jun 28, 2011 at 08:39 AM 0
Share

Do not look for components in Awake or Start. You can not know when they get initialized. Sometimes you might need script 1 to have a reference to script 2, but when you will want script 3 to have a reference to script 2, calling getcomponent in start and not awake or vise versa won't help you.

Call a coroutine with a yield from start and look for components afterwards.

Also: Is script 1 attached to the same gameObject as script 2? Cause if not it will never find it.

avatar image Aldwoni_legacy · Jun 28, 2011 at 08:56 AM 0
Share

thanks for your " also" ;) this works: function Awake() { StartCoroutine(Initiate()); }

private function Initiate()

{ yield; script3 = this.gameObject.GetComponent(script3); object1 = script3.object1; script2 = object1.gameObject.GetComponent(script2); waarde = script2.waarde; }

avatar image
1

Answer by KeithK · Jun 28, 2011 at 08:24 AM

You should only setup references in the Awake function. Like the line below is perfect:

 script1 = this.gameObject.GetComponent(script1);

As for getting information like the value you want from that boolean. Do that in the Start function of Script 2 instead.

 function Start()
 {
     waarde = script1.waarde;
 }

This is because you cannot be sure that the Awake function of Scrip 1 is called before that of Script 2's.

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 Aldwoni_legacy · Jun 28, 2011 at 08:33 AM 0
Share

I still get NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cache$$anonymous$$eyName, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name) UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Really weird behaviour with boolean variables from custom class 2 Answers

Can't get boolean of an animator from other class 0 Answers

NullReferenceException when dealing with Booleans 1 Answer

Null Reference Exception Error...specific error. 1 Answer

Troubleshooting desktop build receiving unhandled NULL exception 0 Answers


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