Refactoring GameSession components

I am currently attempting to refactor player health and score to different scripts and gameObjects as well as UI display. As a result, in addition to wanting only one GameSession in each scene (singleton pattern),
I also want one instance of a player that spawns into a point each scene so that a different instance of player is not required for each different scene. I was wondering what the best approach to this is and what sort of scripts I should be looking to add to achieve this?

Thanks!

Hi Jack,

It’s great to see that you would like to challenge yourself. :slight_smile:

Regarding the starting point of the player and similar problems, always bear in mind that the computer does not know anything about your ideas. For this reason, you always have to explicitely tell the computer what you want, and you also have to explicitely define your concepts/ideas. In the case of the player’s starting/spawn point, you need a variable which has got the spawn coordinates assigned.

Furthermore, when your scene gets instantiated, you want to teleport the player to the spawn point.

Depending on your other ideas, you could either create a dedicated script that teleports the player according to your rules, or you could write a class which initialises certain things in your scene.

Since there is no universal solution for the problems in your game, simply start with what appears to be a good approach to you. If you notice that the approach is not good enough, you can refactor your project again. In fact, we rarely write perfect solutions at the first onset. Instead, we often refactor things multiple times until they are “perfect”.

I don’t know how motivated you are but if you would like to invest a bit more time in your game, I would suggest to take a look at pure C# classes meaning classes that do not inherit from MonoBehaviour or other Unity classes. A pure C# class could improve the performance of your game further. A good example would be a score class, which stores, for example, integers. Or a class that stores “gold coins” (integer). There is no need to look for these objects with FindObjectOfType in the Unity scene because the objects are already there but invisible for us.

And if you need a singleton for the pure C# classes, use the first version from this website.

I hope this helped a bit. Just start with your idea(s) to see what happens. :slight_smile:


See also:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms