0

I seem to be stuck in a catch 22 situation with the OnInspectorGUI method of Unity's UnityEditor class. I want to name array elements in the inspector for easy editing, currently I'm using, as per the documentation:

public override void OnInspectorGUI()
{
   J_Character charScript = (J_Character)target;

   charScript.aBaseStats[0] = EditorGUILayout.FloatField("Base Health", charScript.aBaseStats[0]);

}

In my J_Character script I initialise the aBaseStats array like so:

public float[] aBaseStats = new float[35];

The problem is that whenever I try to do anything in the editor (and thus OnInspectorGUI is called) I get an index out of range error pointing to the line

charScript.aBaseStats[0] = EditorGUILayout.FloatField("Base Health", charScript.aBaseStats[0]);

I'm guessing this is because my array is initialized on game start while the editor code is running all the time while developing.

How can I get round this situation?

Many thanks.

2 Answers 2

4

You have to initialize aBaseStats in an function that runs only once.

The code below is BAD:

public float[] aBaseStats = new float[35];
void Start(){

}

The code below is GOOD:

public float[] aBaseStats;
void Start(){
aBaseStats = new float[35];
}

Initialize it in an Editor callback function that runs once.

EDIT:

I don't know a Start callback function that will run before the OnInspectorGUI function(). The hack below should work.

public float[] aBaseStats;
bool initialized = false;
public override void OnInspectorGUI()
{
   if (!initialized)
   {
       initialized = true;
       aBaseStats = new float[35];
   }

   J_Character charScript = (J_Character)target;

   charScript.aBaseStats[0] = EditorGUILayout.FloatField("Base Health",aBaseStats[0]);

}
Sign up to request clarification or add additional context in comments.

16 Comments

Thanks. I've tried in both Start and Awake but it doesn't work - again, presumably because those functions only run on game start, not in the editor. Can you expand on what you mean by "Initialize it in an Editor callback function that runs once." please?
@Absinthe There are many Editor callback functions and I don't know which one you are using. It would be good if you list all Unity OnSomething functions you are using then I will tell you the right one that runs once.
@Absinthe try something simpler. Try charScript.aBaseStats[0] = 1f; and see if you still get the-same error. If you do then you did not initialize it well. Make sure sure that you initialize it before the line of code charScript.aBaseStats[0] = EditorGUILayout.FloatField("Base Health", charScript.aBaseStats[0]);
charScript.aBaseStats[0] = 1f; didn't work, same error. If I initialize in the OnInspectorGui method there is no error but of course on each call to it any values I assign to the arrays are re-initialized. I've tried looking for some equivalent of MonoBehaviours Awake function for editor code but I can't find anything. The only OnSomething (and indeed the only method) I'm using in the custom editor script is OnInspectorGui
They are not re-initialized. The bool prevents that.
|
0

As an addition to the answer by Programmer I would like to point you to the following:

http://docs.unity3d.com/ScriptReference/ExecuteInEditMode.html

This seems to be exactly what you are looking for in terms of functionality. (it runs the method even when playmode is not active)

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class ExampleClass : MonoBehaviour {
    public Transform target;
    void Update() {
        if (target)
            transform.LookAt(target);

    }
}

2 Comments

Thanks, tried to initialize in Awake and Start with [ExecuteInEditMode] but got the same errors
have you tried combining our answers? If you set a variable to keep track of initialisation and use the update function to initialise your values if they have not yet been initialised it should work. If it doesn't could you post a code sample with the matching error?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.