- Home /
How to I add a collision's GameObject to an array?
The code with all the other bits taken out:
var lGround:Array;
function Start () {
var lGround= new Array();
}
function OnCollisionEnter(other:Collision){
if(other.gameObject.tag == "Ground"){
var gcontainer:GameObject=other.collider.gameObject;
print(gcontainer);
lGround.Add(gcontainer);
}
}
The print line returns (5x): Cube (UnityEngine.GameObject) UnityEngine.MonoBehaviour:print(Object)
Which seems to be the cube object. (There are five of them that it is falling into.) I then get this error (5x): NullReferenceException: Object reference not set to an instance of an object Lemming.OnCollisionEnter (UnityEngine.Collision other) (at Assets/Scripts/Lemming.js:46)
How do I add the object colliding to an array properly?
Answer by ransomink · Mar 17, 2014 at 12:04 PM
You are declaring the var lGround twice: (1) at the top of your script, (2) inside the Start() function. Anything you declare inside of a function will exist only inside the scope of 'that' function. Because of this, you cannot access var lGround = new Array(); outside of the Start() function.
However, you did declare lGround at the top of your script, but, you never created an instance for it, like, lGround = Array();. Inside of your Start() function simply delete "var"
// INSPECTOR VARIABLES //
public var lGroundArray : GameObject[];// a built-in Array (will display in the editor)
//PRIVATE VARIABLES //
private var lGround : Array;// an array holding all ground collision GameObjects
// used for initialization
function Start ()
{
lGround = Array ();// create an instance of the array
Debug.Log ( "lGround Array: " + lGround );// display the array (for reference; it should be empty, of course :P)
}
// check collision for rigidbody GameObjects
function OnCollisionEnter ( other : Collision )
{
// check if the collided GameObject has the specified tag
if ( other.gameObject.tag == "Ground" )
{
var gContainer : GameObject = other.gameObject;
Debug.Log ( "gContainer: " + gContainer.name );// display the collided GameObject
lGround.Push ( gContainer );// add the collided GameObject to the array (which is no longer empty)
Debug.Log ( "lGround Array: " + lGround );// display the contents of the array (updated with the collided GameObject)
/* copy the Unity javascript array into the built-in array.
This will display the contents of the lGround array (your collided GameObjects)
inside the editor AND update it everytime a new collision occurs, so long as the tag matches */
lGroundArray = lGround.ToBuiltin ( GameObject ) as GameObject[];
}
}
Really, don't use Arrays they are slow and not typesafe. Use Lists.
Can you do Lists in Unityscript, I'm not sure if that's an option? If so, please, do tell. Anything to save on speed...
Answer by whydoidoit · Mar 17, 2014 at 10:03 AM
First you should really
import System.Collections.Generic;
And use
var lGround = new List.<GameObject>();
They're faster and typesafe (you will also get intellisense on the members).
Next that line needs to appear outside the Start function because at the moment it is just a local variable that exists in Start! You are not initializing the one in the script (because you added var to the beginning of the initialization line you created a local variable with the same name!)
Your answer
![](https://koobas.hobune.stream/wayback/20220613140427im_/https://answers.unity.com/themes/thub/images/avi.jpg)