2
\$\begingroup\$

This may seem like a dumb question, but I want a single player object for the entirety of my game. However when I load multiple levels obviously I need to reload the player so I would create a player prefab and instantiate him. However, it seems counterintuitive as prefabs are for objects you want to create multiple instances of easily, correct? Also, I need values to be carried over accross levels for my player object therefore I guess I would need a static global singleton to store the player objects.

Hence the dilemna and confusion! Can anyone help me out here?

\$\endgroup\$
5
\$\begingroup\$

Games really aren't about "supposed to" - they're about what works and what doesn't.

If creating a player prefab makes it easier for you to spawn the player avatar in any scene you need them in, and that works for your needs, then it's a good solution.

No Unity police will knock on your door and say "I heard you used a prefab for an object that's only spawned once. That'll be a bajillion dollar fine" - the tools exist for you to get your work done, so use them the way that makes sense to you.

Also, I need values to be carried over accross levels for my player object therefore I guess I would need a static global singleton to store the player objects.

Not necessarily. You could also use DontDestroyOnLoad or additive scene loading to persist one instance of an object throughout your game, rather than unloading & respawning it every time you change to a new level.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Good answer! But still would be nice to hear what is most common or recommended to closer follow to the standards \$\endgroup\$ – Dirk Boer Apr 15 '18 at 9:04
  • \$\begingroup\$ "If creating a player prefab makes it easier for you to spawn the player avatar in any scene you need them in, and that works for your needs, then it's a good solution." — That's about as close to a standard as you're going to find: use Prefabs where they help. \$\endgroup\$ – DMGregory Apr 15 '18 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.