0
\$\begingroup\$

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);
    }
\$\endgroup\$
13
  • \$\begingroup\$ It looks like you already have a potential approach in mind. Have you observed any particular problem or dissatisfaction with this approach, to give a specific axis on which answers can seek to improve it? \$\endgroup\$
    – DMGregory
    Commented Jun 21, 2023 at 4:27
  • \$\begingroup\$ I wanted to see if there are better approaches for handling the behavior I described so that I could try them out and maybe learn the perks/downsides. This is my first attempt at making a game so I am mainly trying to figure out how things work. Reflecting on it the question is quite vague and difficult to respond to. Should I delete it? \$\endgroup\$
    – shanksVR
    Commented Jun 21, 2023 at 5:19
  • \$\begingroup\$ My advice for the first game: focus on finishing it first. There is always some code that can be further improved, made faster, more general or optimazed. As long as you don't notice some horrible lag spikes, don't worry too much at the start and just use the first thing that comes to your mind. \$\endgroup\$
    – Zibelas
    Commented Jun 21, 2023 at 6:06
  • \$\begingroup\$ @Zibelas Got it, I'll try to follow through on that. It's just really hard to stay focused cuz there is so much going on (With unity in general). I have gotten to the point where I know enough to come up with ideas but not enough to do exactly what I want. But I guess I just gotta suck it up and keep it going. Thanks! \$\endgroup\$
    – shanksVR
    Commented Jun 21, 2023 at 6:25
  • \$\begingroup\$ Just one small suggestion, since your SceneManager is a singleton and you carry it to each level, don't hardcode on which scene it can be created. Maybe later you rename your StartScreen scene. Or for debugging purpose you try to load a scene directly. You can find here some extra examples and the differences: gamedev.stackexchange.com/questions/116009/…. Or you want to have it on the StartScreen because the background gets a preloaded level preview. \$\endgroup\$
    – Zibelas
    Commented Jun 21, 2023 at 6:38

1 Answer 1

1
\$\begingroup\$

I would stick with what you have. If it works, it works. Only make changes if you notice a problem or something that is making development in other areas difficult.

You mentioned you are worried about performance. Did you measure it? Is there actually an issue? Did you confirm that the issue is coming from this system? Only worry about performance when those 3 things have already been determined.

Now to get back to your original question. I think your proposed solution looks fine. For a small or medium sized game that’s probably all you need. If you need to massively scale up your game, I would look into using additive scene loading. And keep a persistent scene around in the background with all the persistent objects/UI on them. That way you only load them once and know they’ll always be there.

But. Like I said. Only look into this if you start seeing problems with your initial approach. As the commenters said. Better to finish a game then to have a well designed unfinished game.

\$\endgroup\$
3
  • \$\begingroup\$ Thank you! I'll hold off on worrying about performance until after I am done - I was definitely on track to not completing it haha. \$\endgroup\$
    – shanksVR
    Commented Jun 21, 2023 at 21:07
  • \$\begingroup\$ Haha join the club! I am a member of that club with many projects :). If you feel like the answer answered the question please accept it so future users can learn from your question :) \$\endgroup\$
    – Adam B
    Commented Jun 21, 2023 at 21:34
  • \$\begingroup\$ I'll do my best to get kicked out of the club then, haha. Got it, thanks again! \$\endgroup\$
    – shanksVR
    Commented Jun 21, 2023 at 21:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .