- Home /
How to delay time before pop-up new game object
Hello. I really need help because currently I am doing on this task for my project. How to delay time (seconds) right after the game object has been destroyed? When the game object is destroyed, a new game object will be appeared on the same window. Below is my code:
void OnCollisionEnter (Collision collision)
{
if (collision.relativeVelocity.magnitude>5)
{
AnswerTrue.DestroyAddScore();
Destroy(gameObject);
Instantiate(SpawnParticle,transform.position,Quaternion.identity); //particles appear when the gameobject is destroyed
score+=2; // the score is added when the gameobject is destroyed
Destroy(gameObject);
EndTrue.SetActive(true); // the new gameobject will be appeared as its new window
}
}
I tried to use "IEnumerator" but it does not work very well. The object is destroyed & the score is counted but the new game object does not appear as its new window. Below shows the code that I had edited.
IEnumerator OnCollisionEnter (Collision collision)
{
if (collision.relativeVelocity.magnitude>5)
{
AnswerTrue.DestroyAddScore();
Destroy(gameObject);
yield return new WaitForSeconds(2); // time delay for 2 seconds
Destroy(gameObject);
Instantiate(SpawnParticle,transform.position,Quaternion.identity); //particles appear when the gameobject is destroyed
score+=2; // the score is added when the gameobject is destroyed
Destroy(gameObject);
EndTrue.SetActive(true); // the new gameobject will be appeared as its new window
}
}
I really hope that my question is clear now. I really need your help & thank you so much for your attention.
The function should still complete and Instantiate the object because the Destroy wont happen right away, so I believe that there is an issue with your Instantiate call and not with the ti$$anonymous$$g. You should try instantiating the object in start or update without destroying the object that instantiates it. Just to make sure that your instantiate call is actually working properly. Then move it to the Collision Function.
huh, you destroy the gameObject you're sitting on .. destroy a game object will cancel all its coroutines...
Okay, I already remove that "destroy game object". And I try to put that instantiating in "start" or "update" but still can't work properly. The first coding that I've give is works but the problem is I don't know how to delay the time. So I think it most probably problem on the instantiate part.
You can use OnDestoy() in $$anonymous$$onoBehaviour. When your gameObject destored, call the OnDestory().
Your code add in OnDetroy()
Reference Description
$$anonymous$$onoBehaviour.OnDestroy() This function is called when the $$anonymous$$onoBehaviour will be destroyed.
Answer by Kiwasi · Oct 24, 2014 at 03:07 AM
IEnumerator OnCollisionEnter (Collision collision)
{
if (collision.relativeVelocity.magnitude>5)
{
AnswerTrue.DestroyAddScore();
renderer.enabled = false;
yield return new WaitForSeconds(2); // time delay for 2 seconds
Instantiate(SpawnParticle,transform.position,Quaternion.identity);
score+=2;
EndTrue.SetActive(true);
Destroy(gameObject);
}
}
I've tried to use your code. And it was awesome! I already got what actually I want. Thank you sooo much. :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Fade in AudioLister over time #c 1 Answer
Translating UnityScript to C#. time.delta displays differently 2 Answers