How to access a class/object field In Custom Editor scripts?

Hello,

I would like to know how to acces a class in component (a non monobehaviour class) through a Custom Editor script.

For example I have these:
HouseScript.cs

using UnityEngine;
using System.Collections;

public class HouseScript: MonoBehaviour
{
   public Door door = new Door();
}

HouseScriptEditor.cs

using UnityEngine;
using System.Collections;
using System;
using UnityEditor;

[CustomEditor(typeof(HouseScript))]
public class HouseScriptEditor : Editor
{
    SerializedProperty eDoor;
    void OnEnable()
    {   
        serializedObject = new SerializedObject (target);   
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        serializedObject.ApplyModifiedProperties();
        base.OnInspectorGUI();
    }
}

Door.cs

public class Door
{
   public int someInt;
}

So my question would be, how can I access “someInt” in my Door.cs class?
This might be a stupid question to someone with experience, but this is what I lack from, experience on custom editor scripting.

Thank you for your time :slight_smile:

In HouseScriptEditor.cs:

public override void OnInspectorGUI()
{
    serializedObject.Update();
    serializedObject.ApplyModifiedProperties();
    base.OnInspectorGUI();
    var houseScript = target as HouseScript;
    if (houseScript == null) return;
    houseScript.door.someInt = EditorGUILayout.IntField("Some Int", houseScript.door.someInt);
  }
}

In Door.cs:

[System.Serializable] // Tell Unity to serialize the object data.
public class Door
{
  public int someInt;
}

Thanks, but it seems to not work with arrays. Now I have an array of door classes.
Logically this should work:

if(houseScript.door.Length>0)
houseScript.door[0].someInt = EditorGUILayout.IntField("Some Int", houseScript.door[0].someInt);

but it doesnt, it’s like the array is not initialized. I’m creating the door’s in “Start” method.

Any ideas?

There’s the issue. Unless you use the [ExecuteInEditMode] attribute, Start won’t be called until you play the scene. Could you create the array in Reset()? Better yet, can you initialize it at design time?