• 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 /
This question was closed May 10, 2013 at 07:48 AM by Fattie for the following reason:

Duplicate Question

avatar image
1
Question by wyler0 · May 09, 2013 at 11:20 PM · nullreferenceexceptionbool

Null RefferenceException Error when accessing bool from another script

Really don't need to explain much but basically one script needs to know when the game is over from another script. I have checked all over and I don't believe I am doing anything wrong but I am still getting an error (null reference exception.) Here is my code:

     public bool GameOver;
     
     // Use this for initialization
     void Start () 
     {
         
     }
     
     // Update is called once per frame
     void Update () 
     {
         Movement movementScript = GetComponent<Movement>();
         if(movementScript.GameOver == false)
         {
             GameOver = false;    
         }
         if(movementScript.GameOver == true)
         {
             GameOver = true;    
         }
         Debug.Log(GameOver);
         if(EscClicked == false && GameOver == false)
         {
 
 
 

Didn't copy the entire script on purpose ;) And here is the other script:

 void OnTriggerEnter(Collider other)
     {
         GameOver = true;
         Camera.mainCamera.transform.position = FinishedCameraPosition;
         Camera.mainCamera.transform.rotation = FinishedCameraRotaition;
     }



More than enough of this script ;)

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

1 Reply

  • Sort: 
avatar image
2
Best Answer

Answer by aldonaletto · May 09, 2013 at 11:40 PM

For this to work, the script Movement must be attached to the same object as the script above. If it's attached to a different object, you must prefix GetComponent with a reference to this object - like this:

 public GameObject movementObject; // reference to the object 

 void Update () 
 {
    // make GetComponent search in the object referenced by movementObject:
    Movement movementScript = movementObject.GetComponent<Movement>();
    // that's a much simpler way to copy the GameOver variable!
    GameOver = movementScript.GameOver;    
    Debug.Log(GameOver);
    if (EscClicked == false && GameOver == false)
    ...
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 Owen-Reynolds · May 10, 2013 at 07:01 AM 0
Share

Looks like his GameOver var is in the main (1st) script, so the change would be in the 2nd, trigger, script, which needs to reach out.

avatar image wyler0 · May 10, 2013 at 08:45 PM 0
Share

Thanks guys fixed it!

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

15 People are following this question.

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

Related Questions

What is wrong with my script? 1 Answer

Two questions about building particle systems from scripts 1 Answer

Instanciate Object Problem for Shooting 2 Answers

How do I check a variable of an instantiated object from another instantiated object? 1 Answer

NullReferenceException on animation clips 3 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