setActive on object in separate scene

Hi,

I have 2 scenes a game scene and a menu screen, I have an object visible on the menu which I want to disable when the player loads from level 1 back into the menu. This is the code that I’m using which is attached to the level scene.

SceneManager.LoadScene("sceneSelectBeta");
			GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag ("Level2Block");

			foreach(GameObject go in gameObjectArray)
			{
				go.SetActive (false);
			}

So the scene should switch back to sceneselectBeta and then disable the object with the tag. I suppose the reason at the moment is because my code isn’t influencing that tag when it’s not present in the current scene. Alternatively is it possible to run the code on the menu scene, specifying that if the previous scene was the level scene, to run the code?

Thanks for any help or advice you guys can give. Cheers.

You can create a static variable to check if the player came from the gameScene:

public static bool fromGame = false;

Then somewhere in your menu in a Start method you set the active = to !fromGame, like.

GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag ("Level2Block");
 
foreach(GameObject go in gameObjectArray)
{
       go.SetActive (!fromGame);
}

And in some start of your gameScene you set fromGame to true.

ScriptThatHoldsTheVariable.fromGame = true;

void Start() is always called when a scene loads so the gameObjects active state in your array will be seted according to the fromGame value, when you go back to the menuScene.

Is the game object in the level scene marked as DontDestroyOnLoad()?

Otherwise your code will not be executed. You will then have to DestroyObject(gameObject) manually afterwards.

DontDestroyOnLoad(gameObject);
SceneManager.LoadScene("sceneSelectBeta");
GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag ("Level2Block");
foreach(GameObject go in gameObjectArray) {
    go.SetActive (false);
}
DestroyObject(gameObject);