Can't find GameObject by Script (DontDestroyOnLoad)

Good morning, I’m developing a mobile game right now. I want to find a GameObject with Gameobject.Find() but nothing is found, but I have the object on which the script hangs with another script linked where Dont destroy on Load is untitled. see below.
Thanks in advance

//Other Code
void Start()
{
DontDestroyOnload1();
}

    public void DontDestroyOnload1()
    {
        DontDestroyOnLoad(gameObject);
        if(instance == null)
        {
            instance = this;
        }
        else
        {
            Destroy(gameObject);
        }
    }

//Script whit.Find

  void Update()
{
  Level4Buttons();
}

    public void Level4Buttons()
    {
        if (LevelSlectionsMenu == 1)
        {
            if (getNext.WhichLevel >= 4)
            {
                Level4ButtonIsDeaktivated = GameObject.Find("Cube");
                Level4ButtonIsDeaktivated.SetActive(true);
                Debug.Log("You can Play LV.4");
            }
            else
            {
                {
                    Level4Button = GameObject.Find("Level4-Button");
                    Level4Button.SetActive(false);
                    Debug.Log("You didn´t can play LV.4");
                }
            }
        }
    }

7121422--850339--upload_2021-5-8_13-33-23.png

Ok, but i to didn´t can use GameObject.Find by Active GamObjects only at objects thats to on the Dont destroy on Load Methode.

Your code implies that you are trying to find deactivated objects:

Level4ButtonIsDeaktivated = GameObject.Find("Cube");
Level4ButtonIsDeaktivated.SetActive(true);

DontDestroyOnLoad is not related to the object being active or not.

Ok, thanks.
Sorry for the many questions i only pogramm like a few weeks.
But how i can Fix the Problem?

You have two choices: you put a tag on it and you try to find it by tag. Or you make a class member something like [SerializeField] private GameObject Level4ButtonIsDeaktivated; and drag and drop the game object in the inspector on this field.

1 Like