- Home /
survival shooter game restarting when player dies
Hi guys, in my survival shooter tutorial, my game restarts when the player dies even if the script game over Manager isn't on the hud canvas. also, the animation for game over only stays on the screen for a few seconds. I can't see why the game restarts without the script tells it too.
Answer by Knightsurfer · Jul 29, 2018 at 07:04 PM
Without any code to go off I can't really see the problem enough,
however you could probably do it with :
if (GameObject.Find("canvas").GetComponent<Canvas>().enabled == false
{
Scene loadedLevel = SceneManager.GetActiveScene();
SceneManager.LoadScene(loadedLevel.buildIndex);
}
though I recommend you go with a button click.
I've been following the zombie survival tutorial that unity provides.
this is the code from the gameover script:
using UnityEngine;
using UnityEngine.Scene$$anonymous$$anagement;
public class GameOver$$anonymous$$anager : $$anonymous$$onoBehaviour
{
public PlayerHealth playerHealth; // Reference to the player's health.
public float restartDelay = 5f; // Time to wait before restarting the level
Animator anim; // Reference to the animator component.
float restartTimer; // Timer to count up to restarting the level
void Awake()
{
// Set up the reference.
anim = GetComponent<Animator>();
}
void Update()
{
// If the player has run out of health...
if (playerHealth.currentHealth <= 0)
{
// ... tell the animator the game is over.
anim.SetTrigger(name: "GameOver");
// .. increment a timer to count up to restarting.
restartTimer += Time.deltaTime;
// .. if it reaches the restart delay...
if (restartTimer >= restartDelay)
{
// .. then reload the currently loaded level.
Scene$$anonymous$$anager.LoadScene("$$anonymous$$ainGame");
}
}
}
}
it never reloads the main game, putting in a debug.log it seems the code never reaches into the if statement for loading the scene
Answer by coolbudy1998 · Jul 29, 2018 at 10:22 PM
Try using Time.time!!!
Hi, thank you for the answer. I have tried equaling restart timer to time. time but there is no difference, the code in the if statement still isn't being reached
$$anonymous$$an replace time.deltatime with time.time. Rest all is fine with your code :) Have a good day !
Your answer
Follow this Question
Related Questions
Rotation help! 1 Answer
Populate game objects from a list?? (C#) 1 Answer
Enemy Rotation Problem 1 Answer
How to access variables from an object based on location? C# (Pic inside) 1 Answer