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[];
}
}
↧