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 jcastaldo13 · May 13, 2014 at 06:14 PM · nullreferenceexception

NullReferenceException

Can anyone explain to me why I am getting this error NullReferenceException object reference not set to instance of an object. Im getting this issue in my game so I came to the live training and started watching the Saving and loading data tutorial. Followed it step by step and I am getting the same error i had in my own game.

Here is my GameControl Script attached to a GameControl object

 using UnityEngine;
 using System.Collections;
 
 public class GameControl : MonoBehaviour {
 
     public static GameControl control;
 
     public float health;
     public float exp;
 
 
     void OnGUI()
     {
         GUI.Label (new Rect (10, 10, 100, 30), "Health" + health);
         GUI.Label (new Rect (10, 40, 150, 30), "EXP" + exp);
 
     }
 
 }

Here is the adjust script attached to the main camera

using UnityEngine; using System.Collections;

public class AdjustScript : MonoBehaviour {

 void OnGUI()
 {
     if(GUI.Button(new Rect(10,100,100,30),"Health up"))
     {
         GameControl.control.health += 10;
     }

     if(GUI.Button(new Rect(10,140,100,30),"Health down"))
     {
         GameControl.control.health -= 10;
     }

     if(GUI.Button(new Rect(10,180,100,30),"Exp up"))
     {
         GameControl.control.exp += 10;
     }

     if(GUI.Button(new Rect(10,220,100,30),"Exp down"))
     {
         GameControl.control.exp -= 10;
     }


 }

}

That is it, super simple and identical to the code in the video. I am getting the error but he is not. Please Help!! Thank you

Comment
Add comment · Show 3
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 ThePunisher · May 13, 2014 at 06:40 PM 0
Share

Nothing is initializing GameControl.control. Have you tried dragging the GameControl object into the inspector property for control?

avatar image stevethorne · May 13, 2014 at 06:43 PM 0
Share

He wouldn't be able to drag it into the inspector property since it's a static variable and those aren't serialized in the inspector.

avatar image ThePunisher · May 13, 2014 at 06:47 PM 0
Share

Oh, didn't know you couldn't but that would make sense I suppose. He needs to initialize the control object for sure though.

1 Reply

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

Answer by Kiwasi · May 13, 2014 at 07:32 PM

Add these line to your GameControl

 void Awake (){
     control = this;
 }

Basically you are not assigning a reference to control.

Comment
Add comment · Show 6 · 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 jcastaldo13 · May 13, 2014 at 07:38 PM 0
Share

Awesome!!! Thank you

avatar image ThePunisher · May 13, 2014 at 07:38 PM 0
Share

That would definitely do it.

avatar image AngryCSharpNerd · May 13, 2014 at 08:46 PM 0
Share

You should vote him as the correct answer..

avatar image jcastaldo13 · May 13, 2014 at 08:47 PM 0
Share

I added this and now it works, could anyone tell me why :)

 void Awake()
     {
         if(control == null)
         {
             DontDestroyOnLoad(gameObject);
             control = this;
avatar image meat5000 ♦ jcastaldo13 · May 13, 2014 at 08:52 PM 0
Share

Its the same as Bored$$anonymous$$ormon did but it checks first. The DontDestroy does not contribute to the effect.

avatar image Kiwasi jcastaldo13 · May 13, 2014 at 09:41 PM 0
Share

The key line is

 control = this;

You simply need to place this line anywhere before (in execution order) you try to reference it.

Execution order runs something like this

  1. Every Awake() runs

  2. Every Start() runs

  3. Update() runs every frame

  4. Each time a GameObject is instantiated it runs its Awake() and then Start() before the current Update() countinues

In practice this means:

Awake() should contain all code needed to initialise itself. Don't refer to any other GameObjects during Awake() because they might not exist yet.

Start() should contain everything needed to initialise a GameObject in relation to other GameObjects. Anything set up in awake will be available here.

Update() contains the code you run every frame. Avoid anything with a high performance cost here.

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

25 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Error: NullReferenceException; Object reference not set to an instance of an object 1 Answer

AnimationEvent NullReferenceException 1 Answer

NullReferenceException: Object reference not set to an instance of an object DestroyByContact.OnCollisionEnter2D (UnityEngine.Collision2D coll) 1 Answer

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

Object Reference Not Set To An Instance Of An Object! 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