- Home /
Is it possible to destroy an object at a specific position?
Is it possible to do something like this?
GameObject.Destroy(Object(0,0,0));
Answer by giano574 · Mar 02, 2014 at 04:02 PM
No, that is not possible. You have to know which GameObject is at that position. If you are making a grid-type game, you can make a jagged array which contains a GameObject at each specific index = position.
So if your grid is, say 5 by 4 you will have a jagged array like this:
GameObject[][] objectsInMap = new GameObject[5][]
Then, use a for-loop to initialize each index to 4:
for (int i = 0; i < objectsInMap.Length; i++)
{
objectsInMap[i] = new GameObject[4];
}
Then, when you need to destroy a GameObject at a specific position, use:
if (objectsInMap[x][y] != null)
{
Destroy(objectsInMap[x][y]);
}
This, of couse also applies to 3-dimensional arrays, eg. [][][], but at this point you might want to search for better options, since the array would become huge.
Would it be possible to create Specific GameObjects on a specific region of the grid? If so, how would I go about it?
Answer by TrevorP · Mar 02, 2014 at 04:10 PM
You could create a collider at the desired position, set it to trigger and attach a script with an OnTriggerEnter event method that could handle the object destruction.