Daniel Lip
Daniel Lip

Reputation: 11329

How can I get a reference for a gameobject from another scene?

I have two scenes. Main Menu and the gameplay scene. All my objects of my game are under one parent gameobject called Main Game. And this object is disabled when running the game. First the main menu scene is active.

When I click the PLAY button in the main menu I want to set active true the Main Game object in the other scene.

Main Menu

This script is attached to the Main Menu gameobject :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class MainMenu : MonoBehaviour
{
    public void PlayGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1, LoadSceneMode.Additive);

        var mainGame = GameObject.FindGameObjectWithTag("Main Game");
        mainGame.SetActive(true);
    }

    public void QuitGame()
    {
        Application.Quit();
    }
}

I tried to use FindGameObjectWithTag but the var mainGame is null.

And this script is attached to the Back to main menu gameobject :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class BackToMainMenu : MonoBehaviour
{
    // Variables
    private bool _isInMainMenu = false;
    public GameObject mainGame;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (!_isInMainMenu)
            {
                SceneManager.LoadScene(0, LoadSceneMode.Additive);

                // -- Code to freeze the game
                mainGame.SetActive(false);
            }
            else
            {
                SceneManager.UnloadSceneAsync(0);
                // -- Code to unfreeze the game
                mainGame.SetActive(true);
            }

            _isInMainMenu = !_isInMainMenu;
        }
    }
}

The logic as I think it should be :

  1. Game start with main menu scene.

  2. PLAY button to start a new game.

  3. ESCAPE key to pause/resume the game.

Number 3 when pressing the escape key once it will be back to the main menu and then another pressing on escape key will return and resume the game from the current point either if it's in the middle of a cut scene or just idle in the game.

My first problem is to get the Main Game object reference in the Main Menu scene.

I'm using LoadSceneMode.Additive because I don't want to load each time the other scene but to switch between them that's why all the gameobject of the gameplay scene are under Main Game.

Upvotes: 0

Views: 4628

Answers (1)

Nick Pfister
Nick Pfister

Reputation: 29

Daniel,

One way to achieve this would be to provide a static reference to the Main Game object. This will be accessible cross-scene. For example,

using UnityEngine;

public class GameManager: MonoBehaviour
{
    [SerializeField]
    private GameObject mainGame;

    public static GameObject MainGame {get;private set;}

    void Awake(){
        GameManager.MainGame = mainGame;
    }

}

Now you may refer to the mainGame object in your MainMenu script via GameManager.MainGame

Upvotes: 0

Related Questions