Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Inspector default values on derived Scriptable Object Class

Discussion in 'Scripting' started by Gekigengar, Jul 15, 2019.

  1. Gekigengar

    Gekigengar

    Joined:
    Jan 20, 2013
    Posts:
    706
    Hello, I have found that the default values won't be initialized when creating scriptable object from right click, if it is from a derived scriptable object base class.

    Code (CSharp):
    1. abstract public class StatusEffect : ScriptableObject
    2. {
    3.     [Header("Generic Fields")]
    4.     public string statusEffectName = "Test"; //This will result in blank inspector default values.
    5.     public string description;
    6. }
    7.  
    8. public class SpeedBoostEffect : StatusEffect
    9. {
    10.     [Header("Speed Up Fields")]
    11.     public float baseMSBonus = 2; //This will result in blank inspector default values.
    12.     public float multiplierMSBonus = 0.2f; //This will result in blank inspector default values.
    13. }
    Why wouldn't the default values work like regular fields?

    Thank you.
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    May be .net serializer overrides them or do not call base class field initializers at all. Declare a parameterless constructor and initialize defaults explicitly in constructor.
     
  3. MrRightclick

    MrRightclick

    Joined:
    Nov 14, 2012
    Posts:
    19
    Uweb95, sonmium, Tronyc and 5 others like this.
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    Reset() is better place to set defaults than constructor for consistency since reset also will be called when reset menu item is clicked.
     
    sonmium, estworks and tkshredder like this.
  5. Gekigengar

    Gekigengar

    Joined:
    Jan 20, 2013
    Posts:
    706
  6. abitofjohn

    abitofjohn

    Joined:
    Nov 6, 2012
    Posts:
    27
    Excellent, Reset() was exactly what I was looking for, many thanks!