Activating an object from an other Script,Activating an Object from antoher Script

Hello

how can I activate an object from another script? I have intro which I can deactivate but I want to activate the menu after that which is not working. When I try my following code I get an error.

 using System.Collections.Generic;
 using UnityEngine;

 public class MenuScreen : MonoBehaviour {


 public GameObject MainMenu;

GameObject.Find("MainMenu").active = true;
   

public void MenuScre ()
{
    gameObject.SetActive(false);
 }
 },Hello,

I have a script on an intro animation (just text blending in). After that i want to deactvate the animation with
gameObject.SetActive(false);

which works perfectly fine but i dont know how to enable my Menu. I tried the following but it just gives an error.

 using System.Collections.Generic;
 using UnityEngine;

 public class MenuScreen : MonoBehaviour {


 public GameObject MainMenu;

GameObject.Find("MainMenu").active = true;
   

public void MenuScre ()
{
    gameObject.SetActive(false);
}
}

You cannot put any instructions outside of methods. The method an instruction is in defines when exactly the instruction is supposed to be executed. There are languages where instructions outside of methods mean “execute these at the start of the program” but C# does not support that.

So this line here

GameObject.Find("MainMenu").active = true;

is invalid in its context.

If you want to do something at the start, put it in the Start method:

void Start()
{
    GameObject.Find("MainMenu").SetActive(true);
}

Next up… A GameObject that is not active cannot be found by using GameObject.Find. You’ll have to use something else.


Also a general advice: If you have an error message and want help for it, post the message :wink: