Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Jonas · Apr 09, 2010 at 09:21 AM · gameobjectarrayobjectcontrollerstoring

Array of GameObjects

Hi All.

I'm having some trouble adding and especially retrieving GameObjects from an array. Here is a code sample:

var balls = new Array();

function Start() {

// Instantiate ball for test-purpose
var newBall : GameObject = GameObject.Instantiate(ballPrefab,Vector3(10.0,0.0,10.0),Quaternion.identity) as GameObject;
balls.Add(newBall);

}

function findClosestBall(ball_x : int, ball_y : int) : GameObject {

for (var i=0; balls.length; i++){ var ball : GameObject = balls[i] as GameObject; // THIS MAKES ERROR var ballVector : Vector3 = (ball.transform.position); }

return null;

}

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Jonas · Apr 15, 2010 at 09:15 AM

Problem is solved. Answer can be found at http://forum.unity3d.com/viewtopic.php?t=49078

In short, here is the code:

var balls : ArrayList; 

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;

  if (distanceVector.magnitude < shortestDistance){ 
     closestBall = ball; 
     shortestDistance = distanceVector.magnitude; 
  } 

} return closestBall; }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
4

Answer by duck · Apr 09, 2010 at 09:55 AM

your for statement has an error. It says:

for (var i=0; balls.length; i++) {

when it should say:

for (var i=0; i < balls.length; i++){
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jonas · Apr 09, 2010 at 10:37 AM 0
Share

Yeah of course. That's not the error though.

avatar image
1

Answer by Shadamer · Nov 30, 2013 at 03:43 PM

No, he's right. That would cause the error. If balls.length is not zero, that expression evaluates to true, no matter what iteration you're on. You'll eventually hit the end of the array and be out of objects, hence the null reference.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by ScottPeal · Jan 25, 2014 at 05:28 PM

I like to use

for (var i=0; i < balls.length - 1; i++)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Veith · Jun 01, 2016 at 06:42 PM -1
Share

Are your balls that short?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Storing objects in arrays 2 Answers

Removing objects from an array 2 Answers

finding an objects index in a multi dimensional static array? 1 Answer

How to remove objects from a list ? 3 Answers

GameObject turns into Object in Array 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges