- Home /
How to "Restart level after 2 seconds "
Hi, i'm trying to make the main scene of my game restart after my character dies. But my script just won't work.
It is attached to the trigger on my character head, and is in the same script that makes my character die.
What should i do to make it work? Here it is:
var explosion: Transform;
-
function Start () {
}
function Update () {
}
function OnTriggerEnter (otherObject: Collider)
{
if(otherObject.gameObject.tag == "FallingObject" )
{
Destroy(GameObject.Find("First Person Controller"));
var tempExplosion: Transform;
tempExplosion = Instantiate(explosion,transform.position,transform.rotation);
yield WaitForSeconds(2.0);
Application.LoadLevel(1);
}
}
It is unclear the relationship between this script and the character that dies. If it is on the character that dies, calling Destroy() will destroy this script before it has a chance to load the next level.
That is probably the problem, any idea of how i can solve this?
Answer by Corentin · Feb 05, 2014 at 09:12 PM
If I understand your design, this script is a component of the character (maybe one of the character's children ?). In this case you can destroy the character in this script with Destroy(gameobject);
But this will also destroy your script, and prevent re-loading. You could use an empty GameObject (let's call him "GameFlow") that won't be related to your character (probably at the base level of your hierarchy), and add a script to this "GameFlow".
The script you posted should have a reference to "GameFlow's" script, so that it can do this : gameFlowScript.Invoke(restartLevel,2f);
I understood the point, but i'm not experienced enough to call for a reference, can you write and example for me? $$anonymous$$ore specifically, how should i change my previous script? And what should i attach to the gameflow object?
what I meant was that the script you wrote above could have this member : public GameFlowScript Gfs; So that you can add the reference from Unity's Editor. You should create a GameFlowScript as a new component of an empty GameObject, and it should have this method : public void RestartLevel(){ Application.LoadLevel("mylevel"); }