- Home /
instatiate gameobject after 1 second
--hey guys , after 1 SECOND from when this box is destroyed , i want to instantiate a gameobject.
--note : please i want to instantiate the gameobject after 1 SECOND from when the box was destroyed. thanks !
var fruit : GameObject;
function OnTriggerEnter(other: Collider){
if (other.CompareTag("Spin")){
Destroy(gameObject);
}
}
function OnDestroy () {
var gameobject : GameObject = Instantiate(fruit,transform.position, transform.rotation);
}
Answer by mclankyman · Mar 01, 2014 at 06:19 PM
If you have a game controller game object which handles the instantiation of the Fruit Object and give that game controller a script it should work. It did with this code. I gave the gamecontroller a script called OtherClass.
This game object is destroyed when you click it and will call a method from the other class (which will still exist after you destroy this one).
public class destroy : MonoBehaviour {
public OtherClass other;
void Awake()
{
other = GameObject.FindGameObjectWithTag ("GameController").GetComponent<OtherClass>();
}
void OnMouseDown()
{
Destroy(gameObject);
}
void OnDestroy()
{
other.delayedInstantiate();
}
}
The other class handles the delay.
public class OtherClass : MonoBehaviour{
public GameObject fruit;
public void delayedInstantiate()
{
Invoke("InstantiateFruit", 1f);
}
private void InstantiateFruit()
{
Instantiate(fruit);
}
}
@mclankyman hey dude thanks for the reply , i didn't know how to add it in my script , please would help me with this ?thanks
thanks for the script @mclankyman , but when i destroy the box , the fruit didn't spawn at all :/
It won't because the thing being invoked has been destroyed. You cannot Invoke on the same object - try invoking on another one or delaying the destruction until after the delay (just turn off the renderer and collider).
can you put the CreateFruit function in another script? One that is still alive?
yeah i can but , how can i spawn the fruit after one second from when the box is destroyed ( from another new script) , help ! thanks !
Your answer
![](https://koobas.hobune.stream/wayback/20220613135114im_/https://answers.unity.com/themes/thub/images/avi.jpg)