- Home /
Get the amout of objects in a given position
Hi, I wanted to know how to get the amount of objects that are in a given position. For example, if the position wold be 0, 2, 5 then it would give me the number of objects at this exact point. Does anyone know how to do this? If so I thank you a lot... For more informations, feel free to ask me. (extra note: all the objects are always at a whole numer position)
Answer by Spip5 · Aug 14, 2020 at 10:55 PM
What I did in a grid-based game is, upon instantiation, assign a struct with posX and posY and a reference to the object :
[System.Serializable]
public struct objectPos
{
int x;
int y;
GameObject gO;
}
And I add the structs to a master list in a singleton script. Then with a simple ForEach or For loop, you can go through the list and know how many share the same struct coordinates, and which one they are. Hope that helps!