Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by tom_carr_2 · Dec 29, 2015 at 10:44 PM · gameobjects

identify arrayed game objects

Hi, Programmer new to Unity. I have instantiated game objects to a multi-dimensional array: instantspheres[z1, x1] = (GameObject)Instantiate(spheregreen1, spawnPosition, spawnRotation);

spheregreen1 prefab has a collider with Is Trigger checked, and a script attached, so I click and OnMouseDown event triggers in script. In the script event, how can I determine which object of the array was clicked, ie, what is z1,x1 of this object?

Comment
Add comment · Show 1
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 tom_carr_2 · Dec 30, 2015 at 03:32 AM 0
Share

looks like the only way I can figure is to assign a tag when array game object is instantiated. tags have to be created already (edit/project settings). then you can use the On$$anonymous$$ouseDown event, which has gameObject as a ref to the collider's game object, then extract tag with gameObject.tag.

if anyone has any better technique, great!

2 Replies

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

Answer by tom_carr_2 · Dec 30, 2015 at 08:55 PM

Thanks for taking the time.

I'm using a multi-dim. array because I'm creating a matrix, say 3x3 game objects, so easier to manipulate in code. The way I did it is to step thru nested for loops (z and x) to identify the object and then take actions.

You said it would be better to make objects with a tag. Then a controller script would be directly accessed with a click and actions could be taken. Why do you say this would be a better way? Thanks again.

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 A_Li_N · Dec 31, 2015 at 02:05 AM 0
Share

I didn't say anything about tags :)

I'm not sure I'm following how putting game objects in a 3x3 array makes them easier to manipulate in code, unless you're manipulating them based on those locations (which is backwards, I feel).

Let's say you are doing a rubix cube example, which is a 3x3 matrix (9 total blocks). If you are looking at one face of the cube and click on the top-left block, you wouldn't attempt to reference that block based on '1, 1', you would already have reference to that block by clicking on it. If you wanted to see that it is next to '1, 2' and '2, 1', etc, you would best use variables stored on the instance (x, y, z) to indicate the index of that array

avatar image tom_carr_2 A_Li_N · Dec 31, 2015 at 07:46 AM 0
Share

thanks for your input. I appreciate you taking the time. and I don't know the best way, like any program$$anonymous$$g task, I just came up with a solution.

sorry for bad info: when I'm talking about 3d locations, not x,y,z but like your rubix cube, the location is relative comparison- like color on two squares of rubix cube.

to provide clarity, following the rubix cube example, if the colors are randomly assigned, then when 1,1 is assigned, it does not know what the others will be. I was not talking about a manipulation (my error) but an analysis to deter$$anonymous$$e what the rest of the colors are after the random assignment is done. then when 1,1 is clicked, I use the array to analyze what adjacent colors are.

I felt the design option was to used fixed objects and change the material or instantiate prefabs. I made a random array, then instantiated objects based on the array. the problem then is when I click an object I have to figure out which one I clicked so I can use array to compare. so I added tags when instantiated- the only solution I could think of.

avatar image A_Li_N tom_carr_2 · Jan 02, 2016 at 12:35 AM 0
Share

Co$$anonymous$$g up with a solution is good :) If I were doing something like this, I think I would store their locations on the individual objects as well as the objects in an array. That gives you direct access to their location based on the one you click on so you can identify them in the array.

avatar image
0

Answer by A_Li_N · Dec 30, 2015 at 05:43 AM

Why are you using a multi-dimensional array? And why do you need to track the fact that GameObjects are in the locations they are in the array?

If you are attempting to get the location of the clicked GameObject, it would be better to store that location (z1, x1) on the GameObject itself (or, rather, a script that has those properties), then use Unity to figure out what GameObject you clicked on and reference its locations.

Beyond that, there's Dictionary (using System.Collections.Generic) or looping over the array until you find the one you're looking for; neither option will be overly great compared to flipping things around.

(Let's hope I didn't completely mis-understand what you were asking) :)

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

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

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

Related Questions

checking if gameobject exists 1 Answer

CHANGING SCENE AND MAKE A GAME OBJECT ACTIVE THERE 0 Answers

Spawn objects in a row with same spacing 0 Answers

How to change the shape of gameObjects 2 Answers

Accessing values on a gameobject 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