unity how to access a gameobject from another scene code example
Example 1: reference to gameobject in different scene unity
FindObjectOfType<Canvas>();
Example 2: reference to gameobject in different scene unity
public Canvas menu;
void Start()
{
SceneManager.sceneLoaded += OnSceneLoaded;
SceneManager.sceneUnloaded += OnSceneUnloaded;
}
void OnDestroy()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
SceneManager.sceneUnloaded -= OnSceneUnloaded;
}
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
menu.gameObject.SetActive(false);
}
void OnSceneUnloaded(Scene current)
{
menu.gameObject.SetActive(true);
}