Custom Editor

Im trying to make a Custom Editor, i go to use ‘target’ to access the instance of the script but it doesn’t seem to want to work?

I have my script set up

using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomEditor(typeof(DialogInstance))]
public class DialogInstanceEditor : Editor {

	public override void OnInspectorGUI () {

	}

}

Although when I try to use target.varFromDialogInstance i get this error

error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

Yet if I set this

DialogInstance script = (DialogInstance) target;

I get the following error

error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEditor.Editor.target'

If i set

DialogInstance target;

Then I get this warning

warning CS0108: `DialogInstanceEditor.target' hides inherited member `UnityEditor.Editor.target'. Use the new keyword if hiding was intended

The problem you’re having is because when you click on a gameobject that has the DialogInstance monobehavior attached, your ‘copy’ of the target does not exist yet, which is why you’re getting all of these errors. What I usually do is initialize my local reference to the target in OnEnable (which is fired every time you click on a gameobject that would enable it).

try something like this:

using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomEditor(typeof(DialogInstance))]
public class DialogInstanceEditor : Editor {

    public DialogInstance _target;

    void OnEnable()    
    {
        _target = (DialogInstance)target;
    }

    public override void OnInspectorGUI () 
    {
        // _target should now contain a reference to your monobehavior.
    }

}