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 shophongkong · Mar 21, 2014 at 07:12 AM · error messagenullreferenceexceptioncomponents

NullReferenceException: Object reference not set to an instance of an object

I searched for "NullReferenceException: Object reference not set to an instance of an object" but I don't get it for the code I have below.

Please help take a look at the following code. I'm trying to understand why its giving me this error.

The question is what do I need to do to make object ac not null? What is the correct way to doing this?

NullReferenceException: Object reference not set to an instance of an object

 public class PlayerTrigger : MonoBehaviour {
 
 public GUIText points_text;
     public Access ac;
     void Awake()
     {
     ac = GetComponent<Access>();
     ac.total = 0;
     if (ac == null)
     {
     Debug.Log ("ac is null.");
     }
 }
 
 public class Access : MonoBehaviour
 {    
 
         public int total_score(int point)
         {
         return total += point;
         
         }
         
         public int total
         {
         get {return m_total;}
         set {m_total = value;}
         }
         private int m_total;
         
     
 }


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 robertbu · Mar 21, 2014 at 07:15 AM 2
Share

This code is attempting to the the 'Access' component on the same game object as this script is attached to. The two most likely reasons you are getting a null are:

  • The 'Access' component is not on the same game object as this script...maybe it is on a child object or some other game object.

  • You have two (or more) game objects that have the 'PlayerTrigger' script attached, but not all of those game objects have an 'Access' script attached.

avatar image Fornoreason1000 · Mar 21, 2014 at 07:15 AM 0
Share

simple m_total must be null, which makes total_score null. so set a default value for m_total;

avatar image shophongkong · Mar 21, 2014 at 07:30 AM 0
Share

I'll further test @robertbu's comments. Changed code from @Fornoreason1000's comments but still the same.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by shophongkong · Mar 21, 2014 at 08:47 AM

I've updated to the following. It works now. I removed the part monobehavior and used new Access to create object.

 public class Access 
 {    
 
         public int total_score(int point)
         {
         return total += point;
         
         }
         
         public int total
         {
         get {return m_total;}
         set {m_total = value;}
         }
         private int m_total;
         
     
 }
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 Fornoreason1000 · Mar 22, 2014 at 09:50 AM 0
Share

code speaks 1000 words. run this code, and check for results in your console

 public class Access : $$anonymous$$onoBehaviour
     {   
      
            public int total_score(int point)
            {
            Debug.Log("$$anonymous$$TOTAL is" + m_total.ToString());
            Debug.Log("total is" + total.ToString())
            return total += point;
     
      
            }
      
            public int total
            {
            get {return m_total;}
            set {m_total = value;}
            }
            private int m_total;
      
      
     }

also ac.total should be after the if commands, not before, Why? well your want to check for null be your trying to reference something before that, so what will happen is the compiler will stop running code once it hits the null ac when you access total from it. this means AC could be null but your if block will never actually be run in code. so when you check for null, do it before you do anything else after GetComponent() otherwise you are wasting your time.

hope it helps.

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

21 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

Related Questions

im getting this error: NullReferenceException: Object reference not set to an instance of an object Item.Start () (at Assets/Scripts/Inventory System/Item.js:9) 1 Answer

Device Simulator - Null Reference Error 0 Answers

Base class variables not initialized for sub class? 1 Answer

NullReferenceException - Why? 2 Answers

AddComponent() throwing NullReferenceException 2 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