Created
March 23, 2026 16:45
-
-
Save bsimser/209702d47f96da4c2a5ed7bd11290da8 to your computer and use it in GitHub Desktop.
Handle activating/deactivating objects in a scene on startup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| /// <summary> | |
| /// Add this component to an empty game object and assign other game objects | |
| /// to it for activation/deactivation on start. | |
| /// | |
| /// This is useful for ensuring certain game objects are active/inactive before | |
| /// other components' Start() methods are called, as this component has a default | |
| /// execution order of -1000. | |
| /// </summary> | |
| [DefaultExecutionOrder(-1000)] | |
| public class ActivateOnStart : MonoBehaviour | |
| { | |
| [SerializeField] | |
| private GameObject[] objectsToActivate; | |
| [SerializeField] | |
| private GameObject[] objectsToDeactivate; | |
| private void Start() | |
| { | |
| foreach (var obj in objectsToActivate) | |
| { | |
| obj.SetActive(true); | |
| } | |
| foreach (var obj in objectsToDeactivate) | |
| { | |
| obj.SetActive(false); | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I use this in my main game scenes as I'm constantly turning things on and off in the inspector and forgetting which ones should be enabled/disabled at startup.
Attach this to an empty game object in your scene and drag in things you want to enable/disable at startup. This script has an execution order of -1000 so it should run before anything else thus ensuring your objects will be setup when a scene loads.
Much more useful than having references to UI panels that need to be setup "just the right way" when you're always tweaking things.
Enjoy!