- Home /
Help with Destroy Object
Ok basically there are spawners that spawn multiple versions of a prefab object called "Crate" The "Crate" has this script attached which basically says that when I press the pickup button it will destroy the object so it disappears and shows animated in my hands. Now when the crates spawn they are all called Crate(Clone) with these scripts attached Now when i press Q all the crates destroy but i only want 1 of them to get destroyed how do i fix this? Do i need to attach several versions of the script to several different objects or is there a faster way?
function Update ()
{
if( Input.GetKeyDown ("q") && GameObject.Find("Player").GetComponent(BoxPick).enabled == true)
Destroy(gameObject);
}
Answer by DaveA · Apr 29, 2011 at 06:36 PM
IF this is on every object, they will all 'hear' the "q" key so all will destroy.
You need a way to select a particular crate. Do you do this by mouse-click or 'last created'? This is up to you, but have a 'var picked = false;' at the top, and set it true when it's picked. Then test for that before destroying it.
Your answer
Follow this Question
Related Questions
Deleting all child clones in a GameObject 2 Answers
Why are my Game Objects being destroyed? 0 Answers
Create a new GameObject on a destroyed GameObject position 1 Answer
Find gameobjects by tag, and destroy if in the distance of a certain game object... 1 Answer
How to destroy intiated gameobject prefab from controller script 2 Answers