I have a situation where I have levels in my game and a start screen. For the levels, there is a persistent empty GameObject. My code for setting the game over and level complete canvases active (Also button initializations eg. restart, quit, next level...)is attached to that empty game object. Further, the canvases mentioned are attached to the persistent object.
Would converting my persistence script into a singleton script be the best way to manage the behavior so that the canvases don't land up on the start screen (and unnecessarily slow the game) when I quit a level? I am planning on putting the persistent object in every level to test each level specifically.
As of right now this is what I'm planning on doing unless there are better patterns or suggestions. Any help would be appreciated!
if (instance == null || (SceneManager.GetActiveScene().name != "StartScreen"))
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
GameObject.Find
throughTime
, to almost anything else in theUnityEngine
namespace. Pure C# is fine. Anything that interacts with the scene, engine or other gameobjects will fail. Even if the object isn't marked "Run in inspector", when you launch the game, it'll be loaded into memory and -because the .Net runtime is never unloaded- will persist there across runs. You can get around this with some AppDomain fanciness, but as I said... PITA. – Basic Jun 21 '23 at 21:46