The object of type has been destroyed but you are still trying to access it.

Hi there I have been getting the following error lately. The scene works great when it is loaded first time but when exiting the scene ( exit button with application. Load) and reloading from menu ( load same level with application. Load) this error comes and does not work properly. Thank you in advance hoping could get some direction on this.

here are the scripts.

using UnityEngine;

public class Runner : MonoBehaviour {

   public static int distanceTraveled;
   private static int boosts;
 
    public float acceleration;
    public Vector3 boostVelocity, jumpVelocity;
    public float gameOverY;
 
    private bool touchingPlatform;
    private Vector3 startPosition;
 
    void Start () {
        GameEventManager.GameStart += GameStart;
        GameEventManager.GameOver += GameOver;
        startPosition = transform.localPosition;
        renderer.enabled = false;
        rigidbody.isKinematic = true;
        enabled = false;
    }
 
    void Update () {
        if(Input.GetButtonDown("Jump")){
            if(touchingPlatform){
                rigidbody.AddForce(jumpVelocity, ForceMode.VelocityChange);
                touchingPlatform = false;
            }
            else if(boosts > 0){
                rigidbody.AddForce(boostVelocity, ForceMode.VelocityChange);
                boosts -= 1;
                GUIManager.SetBoosts(boosts);
            }
        }
        distanceTraveled = (int) Mathf.Abs(transform.localPosition.x);
        GUIManager.SetDistance(distanceTraveled);
     
        if(transform.localPosition.y < gameOverY){
            GameEventManager.TriggerGameOver();
        }
    }

    void FixedUpdate () {
        if(touchingPlatform){
            rigidbody.AddForce(acceleration, 0f, 0f, ForceMode.Acceleration);
        }
    }

    void OnCollisionEnter () {
        touchingPlatform = true;
    }

    void OnCollisionExit () {
        touchingPlatform = false;
    }

    private void GameStart () {
        boosts = 0;
        GUIManager.SetBoosts(boosts);
        distanceTraveled = 0;
        GUIManager.SetDistance(distanceTraveled);
        transform.localPosition = startPosition;
        renderer.enabled = true;
        Behaviour halo = (Behaviour)GetComponent("Halo");
        halo.enabled = true; // false
        rigidbody.isKinematic = false;
        enabled = true;
    }
 
    private void GameOver () {
        renderer.enabled = false;
        Behaviour halo = (Behaviour)GetComponent("Halo");
        halo.enabled = false; // true
        SpecialEffectsHelperGreen.Instance.Explosion (transform.position);
        rigidbody.isKinematic = true;
        enabled = false;
    }
 
    public static void AddBoost(){
        boosts += 1;
        GUIManager.SetBoosts(boosts);
    }
}
using UnityEngine;

    public class GUIManager : MonoBehaviour {
 
    private static GUIManager instance;

    public GUIText boostsText, distanceText;
    public GameObject Control;
    public GameObject GameOverObject;
    public GameObject GameOverCam;
    public GameObject Prompt;
 
    void Start () {
        instance = this;
        GameEventManager.GameStart += GameStart;
        GameEventManager.GameOver += GameOver;
        Prompt.SetActive (true);

  
    }

    void Update () {
        if(Input.GetButtonDown("Jump")){
            GameEventManager.TriggerGameStart();
        }
    }
 
    private void GameStart () {

        Control.SetActive (true);
        Prompt.SetActive (false);
        GameOverCam.SetActive (false);
        GameOverObject.GetComponent<GameOverScriptGlide>().enabled = false;
        enabled = false;
    }
 
    private void GameOver () {

        Control.SetActive (false);
        Prompt.SetActive (false);
        GameOverCam.SetActive (true);
        GameOverObject.GetComponent<GameOverScriptGlide>().enabled = true;
        enabled = true;
    }
 
    public static void SetBoosts(int boosts){
        instance.boostsText.text = boosts.ToString();
    }

    public static void SetDistance(int distance){
        instance.distanceText.text = distance.ToString();
    }
}
    public static class GameEventManager {

    public delegate void GameEvent();

    public static event GameEvent GameStart, GameOver;

    public static void TriggerGameStart(){
        if(GameStart != null){
            GameStart();
        }
    }

    public static void TriggerGameOver(){
        if(GameOver != null){
            GameOver();
        }
    }
}

Uhmm, which code is which?

You don’t call Destroy() in any of this code so the culprit isn’t here.

The error seems quite clear - you’ve destroyed a GameObject (or component) but are still trying to use it through a stored reference.

Good catch. Added their labels thanks

hmm wondering that does not application.loadlevel destroy objects?

Any luck here. Even I facing a similar issue.
The scene and all objects work fine when I load the scene the first time.
When I close that level, returns to the menu scene and load the level again,
it gives me the below error.

MissingReferenceException: The object of type ‘GameObject’ 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.