Custom Inspector resets after Unity restarts

Hello Everybody,

I made a simple custom Inspector editor script for my weapon pick up script that I use to pick up all of my weapons in-game, but recently I ran into one issue. I noticed that everytime I play the game the changed variables would reset. I searched the internet for answers and I found one that worked. It was by using ‘EditorUtility.SetDirty’. But this didn’t solve the problem, not really anyway, because everytime I close the Unity and I open it again the changed variables would still reset. I did the same thing I looked in the internet for answers, but this time I couldn’t find anything that would actually work…
Can anyone help me with this? Thank you!

The Editor Script:

using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(WeaponPickUp))]
public class WeaponPickUp_PropertyHolderEditor : Editor
{
    public WeaponPickUp weaponPickUp;

    void OnEnable ()
    {
        weaponPickUp = (WeaponPickUp)target;
    }

    public override void OnInspectorGUI ()
    {
        EditorGUILayout.Space();
        EditorGUILayout.LabelField("Assign Weapon Item", EditorStyles.boldLabel);
        weaponPickUp.item = (Item)EditorGUILayout.ObjectField(weaponPickUp.item, typeof(Item), allowSceneObjects: true);

        EditorGUILayout.Space();
        EditorGUILayout.LabelField("Weapon Type", EditorStyles.boldLabel);
        weaponPickUp.weaponType = (Weapons.weaponType)EditorGUILayout.EnumPopup(weaponPickUp.weaponType);

        if (weaponPickUp.weaponType == Weapons.weaponType.Gun)
        {
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Choose Ammo Type", EditorStyles.boldLabel);
            weaponPickUp.Ammo_Type = (Bullets.Type)EditorGUILayout.EnumPopup("Ammo Type", weaponPickUp.Ammo_Type);
            EditorGUILayout.Space();

            int max = 0;
            switch (weaponPickUp.Ammo_Type)
            {
                case Bullets.Type.AK47:
                    max = ValuesManager.maxAK47Ammo;
                    break;
                case Bullets.Type.MP5:
                    max = ValuesManager.maxMP5Ammo;
                    break;
                case Bullets.Type.M1911:
                    max = ValuesManager.maxHandGunAmmo;
                    break;
                case Bullets.Type.Sniper:
                    max = ValuesManager.maxSniperAmmo;
                    break;
            }

            EditorGUILayout.LabelField("Add Amount", EditorStyles.boldLabel);
            weaponPickUp.Add_Amount = EditorGUILayout.IntSlider(weaponPickUp.Add_Amount, 1, max);
            EditorGUILayout.Space();
        }

        EditorGUILayout.LabelField("Assign Pick Up(-s) SFX", EditorStyles.boldLabel);
        weaponPickUp.weaponClip = (AudioClip)EditorGUILayout.ObjectField("Weapon clip: ", weaponPickUp.weaponClip, typeof(AudioClip), allowSceneObjects: true);
        if (weaponPickUp.weaponType == Weapons.weaponType.Gun)
        {
            weaponPickUp.ammoClip = (AudioClip)EditorGUILayout.ObjectField("Ammo clip: ", weaponPickUp.ammoClip, typeof(AudioClip), allowSceneObjects: true);
        }
        EditorGUILayout.Space();

        EditorGUILayout.LabelField("Sound Setting", EditorStyles.boldLabel);
        weaponPickUp._volume = EditorGUILayout.FloatField("Volume: ", weaponPickUp._volume);
        weaponPickUp._pitch = EditorGUILayout.FloatField("Pitch: ", weaponPickUp._pitch);
        weaponPickUp._randomizeValues = EditorGUILayout.Toggle("Randomize Settings: ", weaponPickUp._randomizeValues);
        EditorGUILayout.Space();

        if (GUI.changed) { EditorUtility.SetDirty(weaponPickUp); }
    }
}

Try AssetDatabase.Refresh() to save your changes to the items.

Hi! Thank you for the reply. I tried using this before, but it didn’t solved my issue. But luckily I managed to solve this problem. I used Begin and End ‘ChangeCheck’ to determent if any change was made to the specific variable and if it was then mark the script as Dirty. This worked like a charm!

1 Like