- Home /
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.
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. :(
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
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);
}
}
}
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! :-)
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?
I told you simple Array... express like this
public GameObject[] = new GameObject[];
Your answer
Follow this Question
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