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*; //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.