- Home /
How can I access GameObject from it's data
Hello, I have hexagon map where every hexagon has it's own coordinates - I can calculate those coordinates from a position of mouse click but then I have to find its index to access in an array of hexagons in my Map controlling script. The Hexagon Map is not exactly grid so simple calculation of the index is not possible. I have added an index parameter to every Hex Cell instance - I, therefore, ask you if I can get that instance from calculated coordinates and then access index parameter of that cell to return to that Map Control script. Hope you understand my question, thank you!
I would simply suggest you to name hexagon instances to the index numbers. That way when you access hexagons from your calculations, you can simply get its name.
I hope it solves the problem.
Hexagonal or not - it's still a grid, and selection method can simplify it to just boxes with added offset for every second row. stackoverflow/hexagonal-grids-how-do-you-find-which-hexagon-a-point-is-in
Answer by bard124 · Jan 01, 2020 at 05:13 AM
andrew-lukasik's or bhavinbhai2707's solutions would work,
Alternatively you could create a Dictionary in order to input the position key and get the referenced object. To create the dictionary you could use a Tag such as "Hex" and use the function GameObject.FindGameObjectsWithTag("Hex") to retrieve a list of objects and add them to the dictionary with their position as their key.
Thank you, I haven't realised that something like dictionaries even existed, thank you. Works fine!
Your answer
Follow this Question
Related Questions
Multiple Fire Points 2 Answers
Prebuilt gameobject bundles without Prefab instancing behavior. 0 Answers
Placing object with ray cast 3 Answers
GetAxisRaw() Ignoring Second Item 1 Answer