karl
1
I'm trying to create an array and keep it updated based on what objects are currently in a trigger. Here is what I have so far, the problem is that I am getting a null reference error when sending objEntering.gameObject to the augmentArray function.
function OnTriggerEnter(objectEntering : Collider)
{
objectsInTrigger++;
augmentArray(objectEntering.gameObject, fishInRange);
Debug.Log("Object name : " + objectEntering.gameObject);
Debug.Log("Obj in trigger " + objectsInTrigger);
}
function augmentArray(objToAdd : GameObject , array : GameObject[])
{
var tempArray = new GameObject[array.length+1];
for(i = 0; i < array.length; i++)
tempArray _= array*;*_
_*tempArray[array.length+1] = objToAdd;*_
_*// Refill the original array etc etc*_
_*}*_
_*```*_
I don't know any null reference problems (unless your array is null to begin with) but I see you're indexing out of range here:
tempArray[array.length+1] = objToAdd;
Did you mean:
tempArray[array.length] = objToAdd;
You might want to use a `List.` instead, it supports adding new items to it so theres no need creating copies of arrays.
import System.Collections.Generic;
// I am no JS guru so you might be able to strip off the new keyword also.
var fishInRange : List.<GameObject> = new List.<GameObject>();
function OnTriggerEnter(objectEntering : Collider)
{
objectsInTrigger++; // OR you could use fishInRange.Count
fishInRange.Add(objectEntering.gameObject);
Debug.Log("Object name : " + objectEntering.gameObject);
Debug.Log("Obj in trigger " + objectsInTrigger);
}