Search Unity

The object of type "GameObject" has been destroyed

Discussion in 'Editor & General Support' started by CyberKn1ght, Oct 25, 2019.

  1. CyberKn1ght

    CyberKn1ght

    Joined:
    Apr 5, 2019
    Posts:
    16
    I have 2 buttons in my options menu, "return to main menu" and "mute music"
    Each of these buttons has a script attached to it, and calls the OnPress() method of the script when it is pressed.
    I also have a main Level object/script that handles all the scene loading and stuff.
    So the script of the main menu button does FindObjectOfType<Level>() in its Start and then calls level.LoadStartScene() in its OnPress().
    The script for the mute button does the same thing but calls level.ToggleMuteMusic().
    So this worked perfectly before, but then I made the level a singleton with the following code:
    Code (CSharp):
    1. public void Awake() {
    2.         InitializeSingleton();
    3.     }
    4.  
    5.     private void InitializeSingleton() {
    6.         if (FindObjectsOfType(GetType()).Length > 1) {
    7.             Destroy(gameObject);
    8.         } else {
    9.             DontDestroyOnLoad(gameObject);
    10.         }
    11.     }
    So now the main menu button works perfectly, but the mute button gives an error; I think this is because in Start() it finds the old level object, and then the one with DontDestroyOnLoad comes in and deletes the old one, but then why does the main menu button work???

    Mute Button Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using TMPro;
    4. using UnityEngine;
    5.  
    6. public class MuteButton : MonoBehaviour {
    7.  
    8.     [SerializeField] string mutedText = "Unmute Music";
    9.     [SerializeField] string unmutedText = "Mute Music";
    10.  
    11.     private Level level;
    12.     private TextMeshProUGUI myText;
    13.  
    14.     public void Start() {
    15.         level = FindObjectOfType<Level>();
    16.         myText = GetComponent<TextMeshProUGUI>();
    17.     }
    18.  
    19.     public void OnPress() {
    20.         if (level == null) {
    21.             Debug.Log("log 1");
    22.         }
    23.         level.ToggleMuteMusic();
    24.         bool muteMusic = level.GetMuteMusic();
    25.         if (muteMusic == true) {
    26.             myText.SetText(mutedText);
    27.         } else if (muteMusic == false) {
    28.             myText.SetText(unmutedText);
    29.         }
    30.     }
    31.  
    32. }
    33.  
    Menu Button Script:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MenuButton : MonoBehaviour {
    6.  
    7.     Level level;
    8.  
    9.     public void Start() {
    10.         level = FindObjectOfType<Level>();
    11.     }
    12.  
    13.     public void OnPress() {
    14.         level.LoadStartScene();
    15.     }
    16. }
    17.  
    Full Error:

    Code (CSharp):
    1. MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
    2. Your script should either check if it is null or you should not destroy the object.
    3. UnityEngine.GameObject.GetComponent[T] () (at C:/buildslave/unity/build/Runtime/Export/Scripting/GameObject.bindings.cs:28)
    4. Level.ToggleMuteMusic () (at Assets/Scripts/Level.cs:74)
    5. MuteButton.OnPress () (at Assets/Scripts/MuteButton.cs:23)
    6.  

    Thanks for your time :)
     
    Last edited: Oct 25, 2019
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Yeah, we'll probably need the code for the mute button, the one with the error (?) and the exact error.
     
  3. CyberKn1ght

    CyberKn1ght

    Joined:
    Apr 5, 2019
    Posts:
    16
    I updated it :)
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Great, I'm glad you got it working! It is generally proper forum etiquette to post your solution so others reading this thread may benefit, they might have the same problem.
     
  5. CyberKn1ght

    CyberKn1ght

    Joined:
    Apr 5, 2019
    Posts:
    16
    It needed gameObject.SetActive(false) before the gameObject.Destroy() in the InitializeSingleton() method of the level
     
    konsic and JeffDUnity3D like this.