#Player Class in Unity

1 messages · Page 1 of 1 (latest)

cold maple
#

Hey, I'm new to using classes in Unity. I'm trying to make a player class for a 2d platformer, so I can initialize the player character every level without having to rely on creating a prefab then setting every variable from scratch every level. ill provide exact code if needed.

the problem, I have multiple scripts on the player character, such as a moving script, an attacking script, a health script, a special dashing script, as well as any that I would add in the future.

some scripts need access to the same variable, such as moving and the special dash needing rigidbody, but attacking and health don't. i'm already not sure what best way to do this is, but as of now I have a "player : monobehaviour" class, as well as the 4 other scripts "moving : player". i feel like there's a better way to do it, but that's what I've been using

the problem is, when initializing the player, I have to call it through one of the scripts, like Player<Moving>(), and then that only sets the variables for that script. i think im missing something obvious here, but ive been coding nonstop for the past 6 hours and my brain is kinda fried.

scenic otter
cold maple
#

i just had an idea in theory that it would be easier to initialize variants of the player under certain circumstances, such as with a different weapon depending on what is selected in the main menu. looking at it now, it's making less sense though.

ill probably go back to using prefabs, unless there's another better way. someone told me to look at scriptable objects, would those do more of what I was thinking?

#

i don't know why, i just don't like the feeling of using prefabs

scenic otter
# cold maple i just had an idea in theory that it would be easier to initialize variants of t...

For different weapons, you'd usually have some kind of equipment system that would toggle between different weapon prefabs/models. Scriptable objects can be used in that system to define the data of each weapons. But so can the prefabs too. Regardless of your system, at the lower level you'll have to use prefabs, wether it's for the character model, the whole character object or the weapon objects.