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
1
Question by Grif · Apr 18, 2014 at 06:26 AM · arraynullreferenceexceptionobject reference

NullReferenceException with an Array

Hi,

I have the infamous NullReferenceException: Object reference not set to an instance of an object with my script.

What my script does:

It adds the mass of an object to an array when this object collides with a trigger. And it removes the mass when the object stop colliding.

Then on the Update function I get all values from the array, sum them up and compare it to a value. If the sum is higher than the value, then bingo (here: destroy the object).

So basically, I'm building a pressure plate.

The issue is on line 28. It seems that Unity doesn't agree with me using the variable "sum" there. I don't understand why, since this variable is declared and not empty.

Can someone explain to me what is wrong and why? Thanks!

 var massToReach : int;
 @HideInInspector
 var list = new Array();
 @HideInInspector
 var sum : int = 0;                    //note that his variable is declared
 
 function Awake()
 {
 this.list.Push (0);                 //trying to put something in the array so it's not empty
 }
 
 function OnTriggerEnter (other : Collider)
     {
     this.list.Add(other.gameObject.mass);
     print("enter");                 //this works
     }
 
 function OnTriggerExit (other : Collider)
     {
     this.list.Remove(other.gameObject.mass);
     print("exit");                     //this works too
     }
 
 function Update()
     {
     for (var i = 0; i<this.list.length;i++)
         {
         this.sum += this.list[i];     //NullReferenceException: Object reference not set to an instance of an object
         print("update");             //works
         if (this.sum >= massToReach)
             {
             print("destroy");         //doesn't work, obviously
             Destroy (this.gameObject);
             }
         }
     }


Yes I know, I don't need to put "this." everywhere. I did it to be 100% sure that the script would use any instance of this script.

Comment
Add comment · Show 1
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 Grif · Apr 19, 2014 at 03:59 PM 0
Share

Nobody knows?

I didn't expect Unity to have such a weird logic and generic error messages which are of no help. I'm quite disappointed. :(

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Tasmia · Apr 18, 2014 at 07:48 AM

you need to define Array size ... as you are adding elements dynamically so use List instead of Array

http://answers.unity3d.com/questions/352415/how-to-put-gameobjects-to-the-list.html

hope this will help you

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 Grif · Apr 18, 2014 at 05:32 PM 0
Share

Hi,

Thank you for your answer. I did what you say, I used "ArrayList", but I still keep getting the same error message (object reference not set to an instance of an object on line 24 below).

Here is the updated code:

 var massToReach : int;
 @HideInInspector
 var list = new ArrayList();
 @HideInInspector
 var sum : int = 0;                    
 
 
 function OnTriggerEnter (other : Collider)
     {
     this.list.Add(other.gameObject.mass);
     print("enter");                 
     }
 
 function OnTriggerExit (other : Collider)
     {
     this.list.Remove(other.gameObject.mass);
     print("exit");                     
     }
 
 function Update()
     {
     for (var i = 0; i<this.list.Count;i++)
         {
         this.sum += this.list[i];     
         print("update");             
         if (this.sum >= massToReach)
             {
             print("destroy");         
             Destroy (this.gameObject);
             }
         }
     }
avatar image
0

Answer by HarshadK · Apr 18, 2014 at 09:30 AM

The reason behind you are getting that error is that the dynamic arrays are for objects. (Reference: Array)

Here's the working code:

 var massToReach : int;
 @HideInInspector
 var list = new Array();
 @HideInInspector
 var sum : int = 0;            //note that his variable is declared
  
 function Awake()
 {
 this.list.Push (0);           //trying to put something in the array so it's not empty
 }
  
 function OnTriggerEnter (other : Collider)
     {
     this.list.Add(other.gameObject.mass);
     print("enter");           //this works
     }
  
 function OnTriggerExit (other : Collider)
     {
     this.list.Remove(other.gameObject.mass);
     print("exit");               //this works too
     }
  
 function Update()
     {
     for (var i = 0; i<this.list.length;i++)
        {
        this.sum += parseInt(this.list[i].ToString());     //NullReferenceException: Object reference not set to an instance of an object
        print("update");         //works
        if (this.sum >= massToReach)
          {
          print("destroy");      //doesn't work, obviously
          Destroy (this.gameObject);
          }
        }
     }

So in order to make the code work I converted the element from the array to string and then to integer on line #28.

And it works! :-)

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 Grif · Apr 18, 2014 at 05:34 PM 0
Share

Hi,

Thank you for your answer too! I copied the line you wrote but I'm still getting the error message...

It seems like Unity doesn't want me to use any variable there. But why?

avatar image Tasmia · Apr 21, 2014 at 10:24 AM 0
Share

I told you simple Array... express like this

public GameObject[] = new GameObject[];

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

Material[] Object reference not set when instantiating 2 Answers

"Null Reference Error" when using a custom class as an array 1 Answer

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

NullReferenceException: Array C# 1 Answer

NullReferenceException weird error 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