Search Unity

Don'tDestroyOnLoad failing to update

Discussion in 'Editor & General Support' started by wmmayfield, Mar 22, 2019.

  1. wmmayfield

    wmmayfield

    Joined:
    Nov 2, 2017
    Posts:
    18
    I have a simple dodge the rocks kind of game. When the player flips their raft three times it's game over. However, I am unable to make it to GameOver because my healthManager script fails to carryover the damage to the new scene. The same thing happens with my score. It fails to carry over to the next scene.

    I've attached my HealthManager, scoreManager and GameController scripts for your convenenince. I am new to Unity and could use all the help you can give.

    William
     

    Attached Files:

  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Please post code directly into the forum using CODE tags instead of asking people to download and open files.
     
  3. wmmayfield

    wmmayfield

    Joined:
    Nov 2, 2017
    Posts:
    18
    Joe,

    Thanks for the heads up. I've attached the scripts below.

    GameController Script
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;

    public class GameController : MonoBehaviour {

    public GameObject [] hazards;
    10 public Vector3 spawnValues;
    public int hazardCount;
    public float spawnWait;
    public float startWait;
    public float waveWait;

    public Text gameOverText;

    private bool gameOver;
    public bool isDead;
    20
    private int score;

    public static HealthManager healthManager;
    public static ScoreManager scoreManager;
    public static GameController instance;

    void Awake ()
    {
    if(instance == null)
    30 {
    isDead = false;
    gameOver = false;
    instance = this;
    }
    else
    {
    GameController.instance.gameOverText = this.gameOverText;
    GameController.instance.isDead = false;
    Destroy (gameObject);
    40 return;
    }
    if (healthManager == null)
    {
    healthManager = GameObject.FindGameObjectWithTag ("HealthManager").GetComponent<HealthManager> (); //sets up the persistent data for health;
    DontDestroyOnLoad (healthManager.gameObject);
    }
    if(scoreManager == null)
    {
    50 scoreManager = FindObjectOfType (typeof(ScoreManager)) as ScoreManager; //sets up the persistent data for score;
    //scoreManager = GameObject.FindGameObjectWithTag ("ScoreManager").GetComponent<ScoreManager> ();
    DontDestroyOnLoad (scoreManager.gameObject);
    }
    }

    void Start ()
    {
    StartCoroutine (SpawnWaves ());
    60 }

    IEnumerator SpawnWaves()
    {
    yield return new WaitForSeconds (startWait);
    while (true) {
    for (int i = 0; i < hazardCount; i++) {
    GameObject hazard = hazards [Random.Range (0, hazards.Length)];
    Vector3 spawnPosition = new Vector3 (spawnValues.x, spawnValues.y, spawnValues.z);
    Quaternion spawnRotation = Quaternion.identity;
    70 Instantiate (hazard, spawnPosition, spawnRotation);
    yield return new WaitForSeconds (spawnWait);
    }
    yield return new WaitForSeconds (waveWait);
    if (gameOver)
    {
    break;
    }
    }
    }
    80 public void PlayerDead()
    {
    Debug.Log ("dead");
    isDead = true;
    Invoke ("ChangeLevel", 1f);
    }
    void ChangeLevel()
    {
    SceneManager.LoadScene (SceneManager.GetActiveScene().buildIndex);
    }
    90
    public void GameOver()
    {
    Debug.Log("gameover");
    isDead = true;
    gameOverText.text = "Game Over !";
    gameOver = true;
    Debug.Log ("animation");
    gameOverText.transform.parent.GetComponent<Animator>().SetBool ("GameOver", true);

    100 }

    public void RestartGame()
    {
    Debug.Log("restart");
    isDead = false;
    gameOver = false;
    GameController.scoreManager.RestartGame();
    GameController.healthManager.RestartGame();
    StartCoroutine (SpawnWaves ());
    110
    }
    113 }

    ScoreManager Script

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class ScoreManager: MonoBehaviour {

    public Text scoreText;
    public int scoreValue;
    10 private int score;

    void Awake ()
    {
    Debug.Log ("score update");
    if (GameController.scoreManager != null) {
    GameController.scoreManager.scoreText = this.scoreText;
    Destroy (gameObject);
    GameController.scoreManager.UpdateScore();
    }
    20 else
    {
    UpdateScore ();
    }
    }
    void Start()
    {
    GameController.scoreManager.UpdateScore();
    }

    30 public void AddScore(int newScoreValue)
    {
    score += newScoreValue;
    UpdateScore();
    Debug.Log (" add score");
    }

    public void UpdateScore()
    {
    scoreText.text = "Score: " + score;
    40 }
    public void RestartGame()
    {
    Debug.Log ("restart score");
    score = 0;
    UpdateScore ();
    }
    47 }
    HealthManager Script

    1. using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;

    public class HealthManager: MonoBehaviour {

    public int Health = 3; // health begins as an int == 3;
    10. public Slider healthSlider; // the slider is the player health bar;
    public Text healthText; // healthText is the UI player health message;


    void Awake ()
    {
    Debug.Log("health update");
    if (GameController.healthManager != null) {
    GameController.healthManager.healthText = this.healthText;
    GameController.healthManager.healthSlider = this.healthSlider;
    Destroy (gameObject);
    GameController.healthManager.UpdateHealth ();
    20 }
    else
    {
    UpdateHealth ();
    }
    }
    void Start()
    {

    GameController.healthManager.UpdateHealth ();
    30 }

    public void TakeDamage (int damage) // Raft flips are counted in void TakeDamage;
    {
    if (GameController.instance.isDead)
    {
    return;
    }
    Debug.Log("damage");
    Health -= damage; // Damage taken;
    40 if (Health <= 0) // If three flips - gameover is called;
    {
    GameController.instance.GameOver ();
    }
    else
    {
    GameController.instance.PlayerDead(); // Calls gameOver after raft has flipped 3 times;
    }

    healthSlider.value = Health; // Health bar - visual of health;
    50 healthText.text ="Health : " + Health; // text above health bar - text of health;
    UpdateHealth ();
    Debug.Log("Health :" + Health);
    }
    public void UpdateHealth() // updates healthText and healthSlider;
    {
    Debug.Log("update health");
    healthText.text = "Health: " + Health;
    healthSlider.value = Health;
    }
    60 public void RestartGame()
    {
    Debug.Log ("restart");
    Health = 3;
    UpdateHealth ();
    }
    66 }