- Home /
Destroying an object in a matrix
So basically, I have a grid 11x11 that is filled completely with an object of type prefab_1.
I have written the code to retrieve the exact coordinates of the position in the matrix (the grid) where I wan't to destroy the object. How do I actually destroy the specific object, not all of them? What's the smartest solution?
I'm writing in C#
Answer by HenryStrattonFW · Jun 03, 2017 at 05:59 PM
Well if its a game object you can just call
Destroy(myMatrix[a,b]);
That will destroy the game object, and leave a null reference at that position in your matrix.
That is how I actually tried the first time I tried to destroy it, and it didn't work at the time. I've tried it again and it actually worked now, I have no idea why didn't it work the first time. Thanks for your answer.
Glad it worked, one thing to remember here (which I forget all the time to be fair) is If your container is storing the component type then you want to make sure to call destroy for the item.gameObject. I frequently forget about that and when you call Destroy on the component type directly, it just removes the component not the rest of the object.
Your answer
