Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to create an enum-like array of classes with shared variables

Discussion in 'Scripting' started by sudoGravel, May 15, 2022.

  1. sudoGravel

    sudoGravel

    Joined:
    May 14, 2022
    Posts:
    5
    First of all, let me explain, it's really simple actually, I'm just having a massive brainfart and cannot figure out how to do this.

    Basically, I want a main class, for example
    character
    which contains mutiple variables which are shared between all characters alike, such as
    int health, int damage, float attackSpeed
    , etc.

    After that I want to create an actual character (e.g.
    Brian
    ) and set his attributes to (x), and another one (e.g.
    Britney
    ) and set her attributes to (y).

    There is also a menu in which both characters are display and the player can switch between them.

    Then the player controller script takes whichever character the player chose and from said declared variables it attributes them to the player, assigns a sprite and the character should switch successfully

    Now, how do I create this character switcher in which all different characters inherit from a class with variables which they all share?

    Help much appreciated
    :D
     
  2. sudoGravel

    sudoGravel

    Joined:
    May 14, 2022
    Posts:
    5
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,606
    Simplest approach would be to put all the character data onto a scriptable object. That way you can just make different assets for each character. Your character selector ultimately lets you select which SO is being referenced by anything else that needs to know about which character is in play, and what their stats are.
     
    Vryken likes this.