- Home /
Objects you use in every scene?
Hi all,
I find myself having certain scripts and objects that I need in every scene. These objects are things like UI components, GameManager scripts etc, and I'm wondering how to do this in the easiest way for myself while developing.
I've read several way forwards, like having a scene that is used for that and doesn't unload. But then it makes debugging hard, because I can't just press play in my normal scenes.
I can also (as I do now) copy them from scene to scene, but this involves manual work, and sometimes I forgot or objects get outdated because I add to them in another scene etc.
So what are the best practices here, and what can I do to speed up development while maintaining sanity? :)
Answer by CodesCove · Dec 09, 2021 at 02:50 PM
Few ideas (I'm sure there might be better ones) :
I usually have a ScriptableObject that uses SceneManager.sceneLoaded to call a delegate method whenever new scene is loaded. The "default" scripts I have stored to a prefab gameobject that is instantiated by this method. The Monobehaviours (scripts) in this Game Object are built in a Singleton pattern so they are not accidentally dublicated. They have all static self-reference so they can be easily accessed from other scripts.
I also make some components so that they will be instantiated to scene when ever they are first called. Example for this pattern: MultiTimer:Monobehaviour (Pseudocode)
private static MultiTimer multiTimer = null;
private void Awake()
{
this.CheckMultiTimerAvailability();
}
private void CheckMultiTimerAvailability()
{
if (multiTimer == null)
{
multiTimer = this;
}
else
{
Destroy(this.gameObject);
}
}
private static void AddTimerToSceneIfNeccessary()
{
if (multiTimer == null) new GameObject("MultiTimer").AddComponent<CC_MultiTimer>();
}
public static void StartTimer(float startDelay, float duration, TimerEvent onStartCallback, TimerEvent onEndCallback, object data = null)
{
MultiTimer.AddTimerToSceneIfNeccessary();
//Do timer stuff
}
Notice that in above pattern the static self reference is private because we want to call the actual static method (MultiTimer.StartTimer) in this class straight away to support the "first call instantiating". Reason for this pattern is that the Timer uses Coroutines cannot be static and needs to be run in a instantiated Monobehaviour.
Notice also: None of the above patterns have a persistent state over scenes. If you have any need for persistent game data then use ScriptableObjects to store them. You can, of course, make the GameObject persist over sence changes by setting it DontDestroyOnLoad. However I have (nowadays) never really needed to use that since I have used Scriptable objects for game data persistence.
Great idea, which I will certainly use going forward. Thanks a lot for taking the time to answer.
Your answer
Follow this Question
Related Questions
Getting the (instantaneous) normal of a meshfilter object 2 Answers
project wide variables and constants 2 Answers
Global array list c# 1 Answer
Problem with world coordinates in scripts? 1 Answer
Static function and variables error 2 Answers