- Home /
Question by
parkerp1000 · Oct 09, 2015 at 09:07 PM ·
unity 5gameobjectrestartgameoverrestart game
How do I make Game Over text stay enabled for a few seconds before the Application Load function runs?
I have this script that reloads the level once it sees that the game over text is enabled and then reloads the level almost instantly I would like to know how to make it stay there for a few seconds...
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class MovementScript : MonoBehaviour {
public Slider healthBarSlider; //reference for slider
public Text gameOverText; //reference for text
private bool isGameOver = false; //flag to see if game is over
public float speed;
void Start(){
gameOverText.enabled = false; //disable GameOver text on start
}
// Update is called once per frame
void Update () {
//check if game is over i.e., health is greater than 0
if (!isGameOver) {
transform.Translate (Input.GetAxis ("Horizontal") * Time.deltaTime * speed, 0, 0); //get input
transform.Translate (0, Input.GetAxis ("Vertical") * Time.deltaTime * speed, 0);
}
}
//Check if player enters/stays on the fire
void OnTriggerStay(Collider other){
//if player triggers fire object and health is greater than 0
if(other.gameObject.name=="Fire" && healthBarSlider.value>0){
healthBarSlider.value -=.011f; //reduce health
}
else{
isGameOver = true; //set game over to true
gameOverText.enabled = true; //enable GameOver text
}
if (isGameOver == true) {
Application.LoadLevel(0);
}
}
}
Comment
Answer by Statement · Oct 09, 2015 at 10:18 PM
Move Application.LoadLevel to a new method, call it using Invoke.
using UnityEngine;
using UnityEngine.UI;
public class MovementScript : MonoBehaviour
{
public Slider healthBarSlider;
public Text gameOverText;
public float speed;
public float levelLoadingDelay = 5f;
private bool isGameOver = false;
void Start()
{
gameOverText.enabled = false;
}
void Update()
{
if (!isGameOver)
{
transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * speed, 0, 0);
transform.Translate(0, Input.GetAxis("Vertical") * Time.deltaTime * speed, 0);
}
}
void OnTriggerStay(Collider other)
{
if (isGameOver)
return;
if (other.gameObject.name == "Fire" && healthBarSlider.value > 0)
{
healthBarSlider.value -= .011f;
}
else
{
isGameOver = true;
gameOverText.enabled = true;
Invoke("LoadNextLevel", levelLoadingDelay);
}
}
void LoadNextLevel()
{
Application.LoadLevel(0);
}
}
Your answer
Follow this Question
Related Questions
Key Arrows into Swipe Input 0 Answers
Move Rigidbody along curve 1 Answer
Model not rendered on Android Lollipop 5.0.1 0 Answers
how to stop the whole game but not the death animation when player dies 3 Answers
Restart Delay 1 Answer