- Home /
Delete a specific instantiated prefab by its vector3 coordinates?
Hi,
I have a Vector3 array (gameBlocks), ten by ten by ten, with each vector3 holding the coordinates for an instance of my prefab GameBlock. The array is storing the individual coordinates, in a sort of grid, but I have also instantiated the GameBlock prefab to each of those coordinates, creating a large block made of 1000 smaller blocks. Maybe I don't need to do both, but I'm between noob and intermediate levels with scripting, and am having trouble understanding how to handle instantiated objects, most specifically deleting an instantiated object, as I don't know well enough how to get the object back and manipulate it. I can delete an object with a mouse click, but struggle to do it entirely in code.
I am at the point now where I want to find all of the GameBlock items that are live in the scene, and that match with my list of vector3 coordinates in a list called ClearBlocks, then to delete each of them from the scene and from the list. Here is the code, below, and note that I have commented out the one line that I just can't seem to get right. This function is in my GameBlock script, which is attached to the GameBlock prefab. The GameBlock prefabs are instantiated by the Chunk class, which holds the gameBlocks array.
I can post more of my code if you need it, but I suspect you won't; let me know, and I'll be happy to do so.
Thanks!
void ClearAdjacents(){
foreach(Vector3 block in ClearBlocks){
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 10; y++) {
for(int z = 0; z < 10; z++) {
Vector3 tempVector = new Vector3(x,y,z);
if (Chunk.gameBlocks[(int)tempVector.x, (int)tempVector.y, (int)tempVector.z] == block){
//GameObject.Destroy(GameBlock(x,y,z)); <---????
ClearBlocks.Remove(tempVector);
count --;
Debug.Log(transVector + "added to ClearBlocks list.");
Debug.Log("List count = " + count);
}
}
}
}
}
}
What do I have to do to get someone to respond to this?
Your answer
Follow this Question
Related Questions
Access a GameObject through it's tag, using a string. 1 Answer
Limiting respawns on scene 1 Answer
GetComponent: Easy help 1 Answer
Destroy(GetComponent(...)) not working 1 Answer
How do I get the Player's Health Script as Component? 1 Answer