Choosing a kind of player before enterting in a game?

HI
So I am planning to make a choosable player for my game.
So here is the dilemme:
I have multiple characters and multiple vehicules.
I would like to choose ONE player and ONE vehicule before playing, and play with them.
So i need to, like, “spawn” them in the game scene.
Which method should I use?
DontDestroyOnLoad()? I think so, for a script to contains parameters…
And after?
Instanciate()? I dont like this method very much… But is it is the only way to spawn the player with the right character and the right vehicule…
And so, if there is an online tutorial or something, I am open. :slight_smile:
Thank you for answer.

If you went with passing parameters on a script attached to a DontDestroyOnLoad object, in your selection scene you would set the parameters. Then you change scenes. In the game scene you would find the DontDestroyOnLoad object. Finding it using a static reference set when the object was created, or finding it based on a tag assigned to the object, are both easy ways to do so.

Then you just instantiate the appropriate prefabs based on the passed parameters. I tend to prefer passing enums so it is obvious what prefab I’m going to grab in code, and then just using switch statements to translate the enum value to a reference to the prefab, but you can use a dictionary, or convert the enum value directly to an index in an array/list instead. How you do that step doesn’t matter that much as long as it works, since you’re only doing it once the performance difference of your choices is a pretty non-issue. Just whatever way is more maintainable for you is probably the better way to approach it.

1 Like

For these sorts of “please stick around” needs I reach for these:

Some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

Unity3D Singleton with Prefab used for predefined data:

These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

1 Like