Unable to edit fields from custom inspector
Hey everyone,
I was trying to organize my item system via a custom inspector. I have never used custom editors before, and may have made an amateur mistake :(
The script is making fields show up in the custom inspector, as expected, but when I try to edit the fields, they will no change. I will select a new value for my enum, but the inspector won't assign it. I can't assign game objects to my Object fields either and the float field is stuck at zero. Any advice is appreciated.
Here is the MonoBehaviour script that the editor script references:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public enum ItemClass{ Weapon, Armor, Consumable }
public class ItemIndex : MonoBehaviour
{
public List<GameObject> item;
public List<float> armor;
public List<float> damage;
public List<ItemClass> itemType;
}
Here is the editor script that creates the faulty fields:
using UnityEditor;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[CustomEditor(typeof(ItemIndex))]
public class ItemIndexEditor : Editor {
public override void OnInspectorGUI()
{
ItemIndex index = (ItemIndex)target;
int totalItems = 0;
GameObject currentPrefab = null;
float currentArmor = 0;
float currentDamage = 0;
ItemClass currentItemType = ItemClass.Weapon;
currentPrefab = (GameObject)EditorGUILayout.ObjectField("Item Prefab", currentPrefab, typeof(GameObject), false);
currentItemType = (ItemClass)EditorGUILayout.EnumPopup("Item Type", currentItemType);
if(currentItemType == ItemClass.Weapon)
{
currentDamage = EditorGUILayout.FloatField("Damage", currentDamage);
}
else if(currentItemType == ItemClass.Armor)
{
currentArmor = EditorGUILayout.FloatField("Armor", currentArmor);
}
if(GUILayout.Button("Create Item"))
{
totalItems += 1;
index.item.Add(currentPrefab);
index.itemType.Add(currentItemType);
}
}
}
The editor script obviously isn't finished, but I would like to fix the fields, before completing the rest of the script. Thanks for any help.
Answer by Cynikal · Sep 02, 2016 at 02:45 AM
If memory serves me correctly, you should add:
if(GUI.changed)
EditorUtility.SetDirty(ItemValueChanged);
In this case, it'd be things like: totalItems, etc.
Your answer
Follow this Question
Related Questions
Custom Inspector Script Resetting Information 0 Answers
Editor Scripts running slow 0 Answers
EDITOR HOW TO GET A TEXT FILE OBJECT OR INSTANCE ID TO FOCUS THE PROJECT WINDOW ON IT 1 Answer
Custom Inspector changes not updating target in Edit Mode 1 Answer
Error BCW0012 'UnityEngine.Application.LoadLevel(String)' is obsolete. Use SceneManager.LoadScene 1 Answer