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 Android Matt · Feb 24, 2013 at 12:07 AM · colliderprogramminggridfind-gameobject

Find a game object using the value of a variable in the script attached to it, or with it's transform.position?

I have grid of instantiated objects, each with a script attached that contains a Vector2 defining the grid position (i.e. x,y). Is it possible to access the game object only by knowing the Vector2 grid coordinates?

It would be very useful to me to be able to send values to functions attached to each object in the grid, based on where it is in that grid.

I have a simple function to convert my grid coordinates into the transform.position of the center of each grid square. I was thinking that one option would be to instantiate an empty object with collider at the transform.position of the grid square in question, and use the hit to return the game object, but I wondered if there may be a more efficient way since this may need to be carried out a at a relatively fast rate.

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

2 Replies

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

Answer by theundergod · Feb 24, 2013 at 12:37 AM

You could also create a 2-dimensional array with the size of said grid and when instantiating the objects assign them to the appropriate position in the array.

 int xSize = 10;
 int ySize = 20;
         
 GameObject[,] gridObjects = new GameObject[xSize, ySize];
     
 GameObject newObject = (GameObject)Instantiate (prefab, vector3Position, quaternionRotation);
 ScriptWithVector2 script = newObject.GetComponent<ScriptWithVector2>();
         
 gridObjects[script.vector2.x, script.vector2.y] = newObject;

Then all you would need to is use: gridObjects[yourVector2.x, yourVector2.y]

To get the desired GameObject.

Comment
Add comment · Show 2 · 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 theundergod · Feb 24, 2013 at 12:44 AM 0
Share

Or obviously a variation of the above, but I think it gets the point across.

avatar image Android Matt · Mar 04, 2013 at 11:16 PM 0
Share

Thanks, I used this principle in the end. I stored the game objects in an array, and I can access that or any of the scripts attached to each game object as and when I need to. If I have any performance issues with using GetComponent's (hopefully not, it's a pretty slow paced turn game), then I can revisit this and look at caching the relevant scripts too.

avatar image
1

Answer by RetepTrun · Feb 24, 2013 at 12:29 AM

Perhaps collect everybody's transform.position in an array or list and then use a foreach loop to compare the positions to the vector3 equivalent of those vector2's.

Or raycast up from each grid square and see if you hit anything

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 Android Matt · Mar 04, 2013 at 11:13 PM 0
Share

Thanks. I think this would work in other circumstances. I actually opted to collect all the game objects in an array (as per theundergod's suggestion) with the same i & j coordinates. I did that because there were multiple scripts attached to each object that I would like to be able to access.

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

11 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 avatar image

Related Questions

Multiple Cars not working 1 Answer

Switching between areas in a scene with triggers 0 Answers

Make object pass through one object and bounce from another 2 Answers

Problems with crouching (FPS/C#) 1 Answer

OnTriggerEnter is running twice when collisions is detected. Couldnt fix. 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