RayCastHit to array of Objects?

When I click on a tile, I want to get that tile’s position in my array. Now I could do this with a hack, but I’d probably regret that later.
I’ve tried comparing the InstanceID, to no avail.

That doesnt really explain what you are trying to do.
If you use hit.gameObject.transform.position you get the position of the tile your ray is hitting…

That’s the position of the tile, but not the tile itself nor it’s position in my array.

I have a list of tiles, and need to know exactly which tile I just clicked.

Is it an array of GameObjects or a List of GameObjects? (You mentioned both.) If it’s a list then you can use List.IndexOf(item) if it’s an array you can use Array.IndexOf(array, item)

Array of Objects (not GameObjects). Array.IndexOf seems like it’d help, except I don’t have the ‘Object’ data, just Collider, Transform, etc (and Objects don’t have a Transform).

Not sure I understand. What is contained in the array and how does that relate to the “tile” gameobject that is being clicked on?

I get it’s an object array, but what’s in it? The GameObjects I assume…

And you need a reference to said gameobject from the hit info?

Well you noted you know you got access to the transform and collider. Guess what, transform and collider references the gameObject.

hit.transform.gameObject

there you go!

I guess he wants to store only x,y,z in his array instead of Object data, but for that he needs IDs in his objects…

I would do it this way:
º I would create an editor script to attach a simple script to each tile, generating unique ID, int or string, for each one.
º Than in the list of x,y,z coordinates I would use that ID as key value to retreave latter the correct transform position for each tile.

The data would look something like Tile[ID] = “1.123|2.456|3.789”. Where ID is the same of the ID script attached to the gameObject tile I have in the scene.
Splitting the string at “|” and converting to float[ ] would give me the value I need to set transform = new Vector3(float[0],float[1],float[2]);
That way wouldn’t need to store the object itself. Dunno if this is what he is trying to do though.

Sigh. Well, I used a hack to get it to work. See, my tiles are exactly 1x1 and start at O, so the position in the array would be:
Mathf.Floor(hit.point)

That’s really dirty. :slight_smile:

You never really answered how your array of objects relates to the GameObject being hit with the Raycast. If the GameObject has an object then why not just wrap the object in a MonoBehaviour and use GetComponent<> to access it?