Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

MissingReferenceException: The object of type 'Text' has been destroyed

Discussion in 'Scripting' started by JAVIERMUNOZ, Mar 30, 2020.

  1. JAVIERMUNOZ

    JAVIERMUNOZ

    Joined:
    Dec 25, 2019
    Posts:
    8
    Hello i am currently working on a brick breaker project and keep getting this error:

    MissingReferenceException: The object of type 'Text' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object.
    So the error shows up after you lose all three lives and restart the game, the game begins like normal everything working but as soon as you lose your first life after the restart the ball doesnt show up again and that error shows up.

    This is the script the error directs me to..

    using System;
    using UnityEngine;
    using UnityEngine.UI;

    public class UIManager : MonoBehaviour
    {
    public Text Target;
    public Text ScoreText;
    public Text LivesText;

    public int Score { get; set; }

    private void Awake()
    {
    Brick.OnBrickDestruction += OnBrickDestruction;
    BricksManager.OnLevelLoaded += OnLevelLoaded;
    GameManager.OnLiveLost += OnLiveLost;
    }
    private void Start ()
    {

    OnLiveLost(GameManager.Instance.AvailiableLives);

    }

    private void OnLiveLost(int remainingLives)
    {
    LivesText.text = $"LIVES: {remainingLives}";

    }

    private void OnLevelLoaded()
    {
    UpdateRemainingBricksText();
    UpdateScoreText(0);
    }

    private void UpdateScoreText (int increment)
    {
    this.Score += increment;
    string scoreString = this.Score.ToString().PadLeft(5, '0');
    ScoreText.text = $"SCORE:{Environment.NewLine}{scoreString}";
    }


    private void OnBrickDestruction(Brick obj)
    {
    UpdateRemainingBricksText();
    UpdateScoreText(10);
    }

    private void UpdateRemainingBricksText()
    {
    Target.text = $"TARGET:{ Environment.NewLine}{ BricksManager.Instance.RemainingBricks.Count} / {BricksManager.Instance.InitialBricksCount}";
    }

    private void OnDisable()
    {
    Brick.OnBrickDestruction -= OnBrickDestruction;
    BricksManager.OnLevelLoaded -= OnLevelLoaded;
    }
    }
     
  2. JAVIERMUNOZ

    JAVIERMUNOZ

    Joined:
    Dec 25, 2019
    Posts:
    8
    this is my first post and im new to game dev so im sorry if the question is a littlt dumb
     
  3. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
    In your 'Awake' function you add this callback/delegate:

    Code (CSharp):
    1. GameManager.OnLiveLost += OnLiveLost;
    When you restart the game your object is destroyed but that delegate is still active. That means it will try to access the 'LivesText' object that no longer exists and you will get that 'missing reference exception' error.

    To fix it you should remove any delegates you add in the 'OnDestroy' function (or you could do it from your OnDisable function but that isn't ideal as you would need to reinitialise them in an 'OnEnable' function).

    e.g.

    Code (CSharp):
    1. private void OnDisable()
    2. {
    3. Brick.OnBrickDestruction -= OnBrickDestruction;
    4. BricksManager.OnLevelLoaded -= OnLevelLoaded;
    5. GameManager.OnLiveLost -= OnLiveLost;
    6. }
     
  4. JAVIERMUNOZ

    JAVIERMUNOZ

    Joined:
    Dec 25, 2019
    Posts:
    8
    Ahhhhhh thank you so much that did the trick and it works perfectly now! THANKS!!!
     
    tonemcbride likes this.