Need help with destroying an instantiated prefab with a button press

Right now I have the prefab instantiating and destroying after 4 seconds but I need to destroy the prefab when the user hits the “return” key. If anyone could help me with this I would be forever grateful! I’m an animator just learning Unity.

var myPrefab : GameObject;

function Update () {

if (Input.GetButtonDown ("q")){

var QKey = Instantiate (myPrefab, Vector3(0,0,2), Quaternion.identity);

Destroy (QKey, 4);

}
}

I tried using this but it ignored the second Input.GetButtonDown and destroyed the prefab right away.

var myPrefab : GameObject;

function Update () {

if (Input.GetButtonDown ("q")){

var QKey = Instantiate (myPrefab, Vector3(0,0,2), Quaternion.identity);

if(Input.GetButtonDown ("return")){

Destroy (QKey);

}
}
var myPrefab : GameObject;

function Update () {
      if (Input.GetButtonDown ("q")){

            var QKey = Instantiate (myPrefab, Vector3(0,0,2), Quaternion.identity);
      }

      if(Input.GetButtonDown ("return")){

            Destroy (QKey);

      }
}

Did you set “q” and “return” at the input settings of your Unity project? If not, the code you would like to use probably is:

Input.GetKeyDown(KeyCode.Q);

Thanks guys but so far these solutions do not help. All my inputs are set up in the input manager.

WAT. how could that not work? appels script should work poifectly. did you use it verbatum? what result are you getting? can you spawn an object but not delete it? that’s very odd.

from your above code, it looks like you weren’t closing off your first if statement with a } before starting your next one. in other words, only when you hit q could unity ever check to see if you hit return. obviously this would never happen.

Copy and pasted Appels script just to make sure… and it didn’t work. The object is spawning but will not destroy with the key press.

Could someone do a quick test and see if they can make Appels script function properly.

one more bump for help

Hi,

Does this work?

var myPrefab : GameObject;
var QKey : GameObject;

function Update () {
      if (Input.GetButtonDown ("q")){
		QKey = Instantiate (myPrefab, Vector3(0,0,2), Quaternion.identity);
      }

      if(Input.GetButtonDown ("return")){
            if (QKey) {
	            Destroy (QKey);
            }
      }
}

I thought you’d need to have QKey declared outside the scope of the Update function in order for it to exist beyond the end of the Update call.

-Chilton

Thanks Chilton but it didn’t take! I can make due with what I have now. Just can’t for the life of me understand why this won’t work!

final bump! I promise

For Chilton’s code to work for you you’ll need an input named q and an input named return.

Change the GetButtonDown to GetKeyDown(KeyCode.Q) and KeyCode.Return and it’ll work.

If you’re not getting any actual errors, then you need to check your input settings and see that your q and return inputs are set to sane values.

I had a similar problem. But I worked around it by attaching a script to my prefab that handles instantiation of a different object and destroys it’s current one. My main issue was using an empty object to hold many small things. But when I tried to instantiate a new prefab it left a clone even though i destroyed the main components. But anyway. Here’s my implementation of basically an object doomsday button.

	//Prefab variables
	var nextController : Transform;//This 
	var currController : Transform;
	private var waitTime;
//respawn function
function Start(){
	//This is so you don't get this script called a billion times a button press
	waitTime = Time.time + 3;
}

function LateUpdate(){
	//if it's been a couple seconds since the spawn.
	if(Time.time > waitTime){
		//check for destroy input, in this case it's F
		if(Input.GetKey("f")){
			//spawn the next player controller or object or whatever
			Instantiate(nextController,new Vector3(currController.transform.position.x
												,currController.transform.position.y+5
												,currController.transform.position.z
												),gameObject.transform.rotation
												);
			//destroy this current object the script is attached to.  Best used on wrapper or container objects
			Destroy(gameObject);
		}
	}
}

Jonny,

Vicenti is right, I assumed you had set those up.

For the purpose of testing code, I always use the explicit key commands.

Mortiis’ suggestion is what I’d use, too…

Input.GetKeyDown(KeyCode.Q);

…instead of…

Input.GetButtonDown (“q”)

This way you don’t have to remember to set up the input named “q”.

-Chilton