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);
}
}
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.
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);
}
}
}