Search Unity

Inherit values from inspector

Discussion in 'Scripting' started by badacsonyigorenyember, Aug 10, 2022.

  1. badacsonyigorenyember

    badacsonyigorenyember

    Joined:
    Jul 3, 2021
    Posts:
    6
    Hey guys!
    I would like to inherit from a class and give the values given in the inspector to to child's class. For example:
    I have a unit class where i have hp, damage, speed, etc. And i have a builder class as a child of the Unit class. When i assign a builder, then I want to create a builder script, give the existing values to the Builder script from the Unit scrip, where I gave the values in the inspector, then delete the Unit script. How could I do this effectively? If nothing works, I could make a constructor in the Unit and give the values there, but it would be nice to use the inspector, because it would be much easier.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,749
    You will make no constructors for Unity-derived objects (MonoBehaviuor and / or ScriptableObjects). I'll let you google for why that's not going to happen.

    Unity is a component architecture for the most part. You may inherit if you insist, but NONE of the methods involved are virtual and they WILL NOT OVERRIDE by default.

    The connection in the Inspector will always be to a particular concrete class, connected via the GUID in the meta file (NOT the classname!) and the name of the serializable fields.

    I suggest you learn how it is typically done and follow that closely at first to avoid running into mysterious errors.
     
  3. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,648
    You could make a inheritable class which is a property of your Unity Component, and do all that stuff with it. As long as the class is Serializable and you set the field to SerializeField, it should be viewable in the inspector.