Character script/GameObject reference advice/best practices

Hi Unity-ers. I’m trying to set up basic character handling in a game, and my mind keeps going in circles about how to best set this up for the future. Hoping people with more experience than I have some advice you wouldn’t mind sharing!

I basically have three pieces (leaving out much of the detail):

  • Prefabs for five character objects; the player will select one when the game starts
  • A character script to handle basic character functionality
  • Eventually, a GameObject instantiation of the prefab representing the character the player selected

Through the course of the game, obviously I’ll need to have access to all three. I am having trouble deciding how to best connect those pieces in a way that isn’t going to make me want to redo it six months from now. The latest I’ve been tooling around with looks something like this:

  • I have a persistent/singleton asset manager script that holds references to each of the prefabs
  • The character class is attached to each of the prefabs
  • A script in each scene would hold a reference to the GameObjects when instantiated

Is that a reasonable pattern? Is there a better practice here I should consider?

Thank you for the help!

I find this difficult because you have to keep it up to date with every scene in your game.

Why not make a special player scene that only has the things above that you care about, and when you load any of your scenes, also additively load the special player scene?

1 Like

That’s a good idea, I’ll noodle around a bit with this, thanks!

This is the kind of area that puts my brain into an infinite loop lol. My first instinct (maybe coming from non-game dev) is, character prefab contains a character class, which holds all character code/data. But then that data ends up tied to the existence of that gameobject, so my next thought is, switch them around - a character class that holds a reference to a gameobject, once instantiated. But that doesn’t seem right either. Probably just have to make the wrong decision, hate life for a while and learn a lesson haha