- Home /
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.
Answer by ScaniX · Aug 17, 2016 at 03:42 PM
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);
Thanks for the suggestion, I've tried implementing this, so in my game scene I have a game object with the script. And then in my menu scene I have DestroyObject(gameObject);
Which does destroy the object but it also makes it so that none of the UI can be interacted with anymore.
No, this whole script is supposed to be attached to the level game object where your script from the question was co$$anonymous$$g from. You really just had to add those two lines. :)
Ahh great, yeah it's working perfectly. do you by any chance know how I can save the setActive state using PlayerPrefs, at the moment the gameObject becomes active each time the game is reloaded, but it should stay hidden.
Answer by EDevJogos · Aug 17, 2016 at 03:43 PM
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.
Thanks, this seems like a good way to do it, I'm unsure how to implement it though,
public static bool fromGame = false; - I have this in my game scene
then in the start function on my menu script I have the script to find the tag and set active.
I'm unsure where ScriptThatHoldsTheVariable.fromGame = true; fits in though.
to access a static variable from another script in another scene you have to first say which script it belongs too:
Like if you declared public static bool fromGame = false;
inside $$anonymous$$enu.cs than to call it on Game.cs you have to type:
$$anonymous$$enu.fromGame = true;
The ideia is that if you have started the gameScene you change this variable to true, so that when you go back to the menuScene your objects will be seted to false. since the setActive do the opposite of the fromGame value:
go.SetActive (!fromGame);