- Home /
Custom inspector, cant edit variables in subclass
So i used a tutorial on how to create a custom inspector because i'm making a farm game and i wanted to make an easier way to create orders for an order board so i have it to where when i press a button on the inspector it adds a Element to a list and under the Elements are multiple variables
public class OrderAction : MonoBehaviour {
//MyClass myclass;
materialss material;
public List<materialss> materials = new List<materialss>();
[System.Serializable]
public class materialss
{
public string MaterialName = string.Empty;
public int amountNeeded;
public Image materialImage;
public Text amountNeededT;
public void Update()
{
Debug.Log("Done");
materialImage.sprite = Resources.Load(MaterialName) as Sprite;
}
}
}
This is the script for controlling the orders
[CustomEditor(typeof(OrderAction))]
public class OrderEditor : Editor
{
OrderAction t;
SerializedObject GetTarget;
SerializedProperty thisList;
void OnEnable()
{
t = (OrderAction)target;
GetTarget = new SerializedObject(t);
thisList = GetTarget.FindProperty("materials");
}
public override void OnInspectorGUI()
{
OrderAction myOrderAction = (OrderAction)target;
GetTarget.Update();
//myOrderAction.experience = EditorGUILayout.IntField("Experience", myOrderAction.experience);
GUILayout.Label("List of materials");
GUILayout.Space(20);
if (GUILayout.Button("Add Material"))
{
t.materials.Add(new OrderAction.materialss());
Debug.Log("Added");
}
base.OnInspectorGUI();
}
public void addMaterial()
{
OrderAction myOrderAction = (OrderAction)target;
myOrderAction.materials.Add(new OrderAction.materialss());
Debug.Log("Added");
}
}
This is the script that edits the inspector
Now the problem is in the OrderAction script i have a subclass called materialss. When the button is pressed it creates a new element using the variables in the subclass and it creates the new elements fine but i cant seem to be able to edit them. for example I have the Update function and im trying to get the image for the material to change to a sprite according to the name and it doesn't work. i put a Debug.Log in and it doesn't even call that. So how would i be able to edit the variables or can i?
Please look up in the userguide how to properly format code here on Unity Answers. I fixed it for you this time.
Answer by Bunny83 · Nov 29, 2016 at 10:56 PM
First of all you're mixing to different concepts how custom inspectors can be implemented. You could drop your SerializedObject / SerializedProperty as you don't use it at all.
When using the "target" property you're dealing with your object directly. However that means you have to take care of telling the editor when things have changed so the editor actually uptates the serialized data. This is usually done by using Undo.RecordObject right before you change anything.
So it would look like this:
[CustomEditor(typeof(OrderAction))]
public class OrderEditor : Editor
{
OrderAction t;
void OnEnable()
{
t = (OrderAction)target;
}
public override void OnInspectorGUI()
{
if (GUILayout.Button("Add Material"))
{
Undo.RecordObject(t, "Add new material");
t.materials.Add(new OrderAction.materialss());
}
base.OnInspectorGUI();
}
}
public class OrderAction : $$anonymous$$onoBehaviour {
materialss material;
public List<materialss> materials = new List<materialss>();
[System.Serializable]
public class materialss
{
public string $$anonymous$$aterialName = string.Empty;
public int amountNeeded;
public Image materialImage;
public Text amountNeededT;
public void Update()
{
Debug.Log("working");
}
}
}
its still not working. the Debug.Log("working") isnt happening. is there a certain way to perform functions inside a subclass?
Uhm, what does your Update method has to do with the custom inspector? It's just a normal method, if you want to call it, call it. Unity won't call it for you as that class is not a $$anonymous$$onoBehaviour.
If you want to invoke Update when the new instance is created you can do:
var inst = new OrderAction.materialss();
t.materials.Add(inst);
inst.Update();
ins$$anonymous$$d. Though if that method is ment only for initializing it should be named "Init()" or something like that.
The custom inspector part works fine. But its not letting me change things. Like the custom inspector is to create new items that are needed for the different orders in the game. So it has the name of the item, the icon, and the amount needed. and for example i want to change the text for how much wheat is needed for the order but when i try to nothing happens at all. Or if i'm trying to check to see if my inventory contains the amount needed it just wont do anything. Sorry i'm trying to explain it the best i can.