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

Why can't I serialize anything with my custom inspector?

Discussion in 'Scripting' started by candlemaster, Feb 19, 2015.

  1. candlemaster

    candlemaster

    Joined:
    Mar 12, 2014
    Posts:
    16
    So I've written a custom inspector for a couple of my classes, to make them more presentable and to show some information mid-game that would otherwise be hidden. Problem is, no matter what I change in the inspector, as soon as I hit "Play" everything snaps back to default values. No matter what I do, nothing works.

    Here are two scripts that I'm having problems with.
    This is the source code for the class:
    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. [Serializable]
    7. public class Skills : MonoBehaviour
    8. {
    9.     public int startingExp = 0;
    10.     public int maxLevel = 100;
    11.  
    12.     public float expCurveConst = 0.0f;
    13.     public float expCurveScalar = 80.0f;
    14.     public float expCurveSquare = 15.0f;
    15.     public float expCurveCube = 5.0f;
    16.    
    17.     public int ExpReqForLevel(int level)
    18.     {
    19.         return (int)(expCurveConst    +
    20.                      (level * expCurveScalar) +
    21.                      (level * level * expCurveSquare) +
    22.                      (level * level * level * expCurveCube) );
    23.     }
    24.    
    25.    
    26.    
    27.     [SerializeField]
    28.     private int[] _lvl = new int[(int)SkillName.MAX_SKILLS];
    29.     [SerializeField]
    30.     private int[] _exp = new int[(int)SkillName.MAX_SKILLS];
    31.    
    32.     public void Awake()
    33.     {
    34.         for (int i = 0; i < (int)SkillName.MAX_SKILLS; ++i)
    35.         {
    36.             _exp[i] = startingExp;
    37.             UpdateSkill(i);
    38.         }
    39.     }
    40.    
    41.     public int GetLevel(int index)
    42.     {
    43.         return _lvl[index];
    44.     }
    45.    
    46.     public void SetLevel(int index, int value)
    47.     {
    48.         if (value <= 0)
    49.             _exp[index] = 0;
    50.         else if (value >= maxLevel)
    51.             _exp[index] = ExpReqForLevel(maxLevel -1);
    52.         else
    53.             _exp[index] = ExpReqForLevel(value -1);
    54.        
    55.         _lvl[index] = value;
    56.         UpdateSkill(index);
    57.     }
    58.    
    59.     public void GainLevel(int index, int value)
    60.     {
    61.         value = _lvl[index] + value;
    62.        
    63.         if (value <= 0)
    64.             _exp[index] = 0;
    65.         else if (value >= maxLevel)
    66.             _exp[index] = ExpReqForLevel(maxLevel -1);
    67.         else
    68.             _exp[index] = ExpReqForLevel(value -1);
    69.        
    70.         _lvl[index] = value;
    71.         UpdateSkill(index);
    72.     }
    73.  
    74.     public int GetExp(int skill)
    75.     {
    76.         return _exp[skill];
    77.     }
    78.  
    79.     public void GainExp(int skill, int expGained)
    80.     {
    81.         _exp[skill] += expGained;
    82.         UpdateSkill(skill);
    83.     }
    84.  
    85.     public void SetExp(int skill, int newExp)
    86.     {
    87.         _exp[skill] = newExp;
    88.         UpdateSkill(skill);
    89.     }
    90.    
    91.     private void UpdateSkill(int skill)
    92.     {
    93.         // Clean up boundary cases //
    94.         if (_lvl[skill] > maxLevel)
    95.             _lvl[skill] = maxLevel;
    96.         if (_lvl[skill] < 0)
    97.             _lvl[skill] = 0;
    98.  
    99.         // If we've reached the amount of exp we need, level up! //
    100.         while (_exp[skill] >= ExpReqForLevel(_lvl[skill]) )
    101.         {
    102.             _lvl[skill]++;
    103.         }
    104.  
    105.         // If our xp is too low for the level we ought to be at, level down :( //
    106.         while ((_lvl[skill] > 0) && (_exp[skill] < ExpReqForLevel(_lvl[skill] -1)))
    107.         {
    108.             _lvl[skill]--;
    109.         }
    110.     }
    111. }
    112.  
    113. public enum SkillName
    114. {
    115.     Brawling,
    116.     Precision,
    117.     Necromancy,
    118.     Discipline,
    119.  
    120.     MAX_SKILLS
    121. }
    And the corresponding code for the editor:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System;
    4. using System.Collections;
    5.  
    6. [CustomEditor(typeof (Skills))]
    7. public class SkillsEditor : Editor
    8. {
    9.     bool showskills = true;
    10.     Skills skills;
    11.  
    12.     void OnEnable()
    13.     {
    14.         skills = (Skills)target;
    15.  
    16.         if (!Application.isPlaying)
    17.         {
    18.             skills.Awake();
    19.         }
    20.     }
    21.  
    22.     public override void OnInspectorGUI()
    23.     {  
    24.         skills.startingExp = EditorGUILayout.IntField("Starting EXP", skills.startingExp);
    25.         skills.maxLevel = EditorGUILayout.IntField ("Max Level", skills.maxLevel);
    26.  
    27.         // The multipliers for the exp-for-level polynomial function. //
    28.         EditorGUILayout.LabelField("EXP Curve");
    29.         EditorGUILayout.BeginHorizontal();
    30.         {
    31.             skills.expCurveConst = EditorGUILayout.FloatField(skills.expCurveConst);
    32.             skills.expCurveScalar = EditorGUILayout.FloatField(skills.expCurveScalar);
    33.             skills.expCurveSquare = EditorGUILayout.FloatField(skills.expCurveSquare);
    34.             skills.expCurveCube = EditorGUILayout.FloatField(skills.expCurveCube);
    35.         }
    36.         EditorGUILayout.EndHorizontal();
    37.  
    38.         // Display and allow the developer to modify individual skills. //
    39.         showskills = EditorGUILayout.Foldout(showskills, "Show skills");
    40.         if (showskills)
    41.         {
    42.             EditorGUILayout.BeginHorizontal();
    43.              EditorGUILayout.Space();
    44.              EditorGUILayout.LabelField( "Skill Name", "Level" );
    45.              EditorGUILayout.LabelField( "Experience" );
    46.             EditorGUILayout.EndHorizontal();
    47.  
    48.             for (int i = 0; i < (int)SkillName.MAX_SKILLS; ++i)
    49.             {
    50.                 EditorGUILayout.BeginHorizontal();
    51.                  EditorGUILayout.Space(); EditorGUILayout.Space();
    52.                  skills.SetLevel(i, EditorGUILayout.IntField( ((SkillName)i).ToString(), skills.GetLevel(i) ));
    53.                  skills.SetExp(i, EditorGUILayout.IntField( skills.GetExp(i)) );
    54.                 EditorGUILayout.EndHorizontal();
    55.             }
    56.         }
    57.     }
    58. }
    59.  
    Even when I modify public variables like maxLevel in the inspector, it always jumps back to default the second it loads.

    What the heck is going on here?
     
  2. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    556
  3. candlemaster

    candlemaster

    Joined:
    Mar 12, 2014
    Posts:
    16
    Ha! It worked! Thank you so much!