|
Hi All. I'm having some trouble adding and especially retrieving GameObjects from an array. Here is a code sample:
function Start() {
} function findClosestBall(ball_x : int, ball_y : int) : GameObject {
} The error I'm getting is: NullReferenceException: Object reference not set to an instance of an object What am I doing wrong? What is the correct way to store an array of GameObjects? Thanks, Jonas
(comments are locked)
|
|
Problem is solved. Answer can be found at http://forum.unity3d.com/viewtopic.php?t=49078 In short, here is the code:
function Start() { balls = new ArrayList(); // Instantiate ball for test-purpose var newBall = Instantiate(ballPrefab,Vector3(10.0,0.0,10.0),Quaternion.identity); balls.Add(newBall.gameObject); // Notice .gameObject } function findClosestBall(ball_x : int, ball_y : int) : GameObject { var closestBall : GameObject; var shortestDistance : float = 9999; var ballPos : Vector3; var coordinatePos : Vector3; var distanceVector : Vector3; for(var ball : GameObject in balls){ ballPos = ball.transform.position; coordinatePos = Vector3(ball_x, GameController.y_height, ball_y); distanceVector = ballPos - coordinatePos;
} return closestBall; }
(comments are locked)
|
