Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Rybo5000 · Mar 01, 2013 at 01:48 AM · gameobjectcolliderarraylistarrays

How To Get A Reference To All Nearby GameObjects?

I'm trying to get an array of all the nearby GameObjects, so I did this:

 List<GameObject> nearbyObjects nearbyObjects = new List<GameObject>();
 
 float boxLength = minBoxLength + (speed/maxSpeed) * minBoxLength;
         
 Collider[] tempNearbyObjects = Physics.OverlapSphere (transform.position, boxLength);
         
 foreach(Collider tempObj in tempNearbyObjects)
 {
 nearbyObjects.Add(tempObj.transform.parent.gameObject);
 }

But I get a null reference error, which would imply that the tempNearbyObjects array is empty but when I print the length, it's length is 4. (which is how many objects are near that point so it's working).

Is there a problem with how I'm getting the gameObject from the Collider?

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

1 Reply

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

Answer by Julien-Lynge · Mar 01, 2013 at 02:02 AM

This line does not work:

 List<GameObject> nearbyObjects nearbyObjects = new List<GameObject>();

did you type in your code, rather than copying and pasting it?

I'm guessing this is the line that's throwing the error:

 nearbyObjects.Add(tempObj.transform.parent.gameObject);

You are making the assumption that every tempObj has a parent - that is probably not a wise assumption.

Comment
Add comment · Show 3 · 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 Rybo5000 · Mar 01, 2013 at 02:06 AM 0
Share

Yeah I typed out that code just to avoid someone saying "you didn't declare nearbyObjects" - it's not meant to be there twice.

Anyway I don't know if the objects have parents, they're just cubes from the editor, so how do I get the gameObject without a parent? Just tempObj.transform.gameObject?

avatar image Rybo5000 · Mar 01, 2013 at 02:06 AM 0
Share

I just removed the .parent. and now it works! Thank you!

avatar image Julien-Lynge · Mar 01, 2013 at 02:09 AM 0
Share

No worries, glad I could help.

Good luck.

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to create an array of Transforms 1 Answer

How to remove objects from a list ? 3 Answers

Can't add GameObjects to ArrayList 1 Answer

Keep adding targets to a list 2 Answers

How to add and access objects 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