- Home /
How to activate gameObjects based on a boolean from another scene?
Hello! I'm creating a game that includes a campaign mode. I have a button that sends you to a different scene. On the second scene there are 3 buttons; Menu, fail, and succeed. The buttons I press directly correlate to a boolean. I have a DontDestroyOnLoad script, so the boolean already follows you through scenes. My only issue is that I cant figure out how to activate an object based on the boolean in a different scene.
Dont Destroy Script (works): https://pastebin.com/YzQjZ7Mv
--Fail or Succeed script (works): https://pastebin.com/rPGDhzJ4
--Checks for the boolean (Seperated from main script): https://pastebin.com/nx6r9KtJ
In that last pastebin you will find the script that WORKS, but I cant assign anything to. If I assign that script to anything, I don't get a slot to add a reference. I obviously need the reference, it will throw errors otherwise. I only started coding about a week ago, so I could really use any help I could get. Sorry if this is a dumb question, let me know if you need any more information (I probably missed some.) Thank you!
I don't know if this is your problem, but the command to find a object via tag takes in a string, so you should write the TagName in "". If this throws an error you won't get the slot for the ScoreKeeper. Another issue I guess is, that you use the scorekeeper in Awake before giving it a value in Start. Awake is called before Start. I'm however not sure if I got everything right, so this is just a comment.
Im guessing that you should GameObject.Find() the gameObject you want to enable/disable after the scene changes, listening to OnSceneLoaded()
Your objects will be destroyed as soon as you load a new scene, so you can no longer get a reference to your script. What you should do instead is make the boolean static, so it persists through the application lifetime.
Answer by jaumeig95 · Aug 11, 2020 at 07:54 AM
You can use a static class in your game assets. It doesn't extenda from Monobeheavour and only has variables and methods. You don't atach this script to objects, you let it in the script folder.
Imagine your script name is called "GameManajer"
So you can use in the other scripts
Gamemanajer.MyBooleanVariable = true;