- Home /
Custom editor Error?
Hey, i have been working on my first custom editor but i keep getting(indexoutofrange) error for one of my array, even tho the index i give it was 2 and it has 15 indexes, I Have tried all sorts of things but carnt get it to work, any help would be great!
Main script :
using UnityEngine;
using System.Collections;
public class GodsToolGenerator : MonoBehaviour {
public int[] noiseType = new int[15];
public float[] noiseInfluence = new float[15];
public Noise noise = new Noise();
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Editor script :
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(GodsToolGenerator))]
public class GodsToolEDITOR : Editor
{
int numberOfLayers = 1;
bool enableHelp = true;
bool unFoldLayers = false;
string[] optionForNoiseType = new string[5]{"Perlin","Simplex","Turbulance","Wood","Marble"};
public override void OnInspectorGUI()
{
GodsToolGenerator god = (GodsToolGenerator)target;
//Main Layout#
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel("Enable Help? ");
enableHelp = EditorGUILayout.Toggle(enableHelp);
EditorGUILayout.EndHorizontal();
EditorGUILayout.LabelField("Noise Options");
if(enableHelp==true){
EditorGUILayout.HelpBox("Here you can choose how many layers of noise to use.",MessageType.Info);
}
EditorGUILayout.Space();
EditorGUILayout.LabelField("Number Of Noise Layers");
numberOfLayers = EditorGUILayout.IntSlider(numberOfLayers,1,15);
EditorGUILayout.Space();
//Noise Type
unFoldLayers = EditorGUILayout.Foldout(unFoldLayers,"Layer Options");
if(unFoldLayers==true){
if(enableHelp==true){
EditorGUILayout.HelpBox("Here you can choose what type of noise to use for each layer.",MessageType.Info);
}
int testVal = 0;
for(int i = 0 ; i < numberOfLayers ; i++){
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel("Layer " + (i+1).ToString() + " Noise Type");
god.noiseType[2] = 1;//(int)EditorGUILayout.Popup(god.noiseType[2],optionForNoiseType);
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.Space();
if(enableHelp==true){
EditorGUILayout.HelpBox("Here you can choose the influence of each layer.",MessageType.Info);
}
//Make layers
for(int i = 0 ; i < numberOfLayers ; i++){
EditorGUILayout.FloatField("Layer " + (i+1).ToString() + " influence", i+1);
}
}
EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.LabelField("Generation Options");
//GUILayout.Button("Generate");
//GUILayout.Button("Reset To Defauts");
}
}
And finally the error :
IndexOutOfRangeException: Array index is out of range. GodsToolEDITOR.OnInspectorGUI () (at Assets/Editor/GodsToolEDITOR.cs:65) UnityEditor.InspectorWindow.DrawEditors (Boolean isRepaintEvent, UnityEditor.Editor[] editors, Boolean eyeDropperDirty) (at C:/BuildAgent/work/d3d49558e4d408f4/Editor/Mono/Inspector/InspectorWindow.cs:850) UnityEditor.DockArea:OnGUI()
Also tested it just like this but get same error :
using UnityEngine;
using System.Collections;
public class GodsToolGenerator : $$anonymous$$onoBehaviour {
public int[] noiseType = new int[15];
public float[] noiseInfluence = new float[15];
public Noise noise = new Noise();
// Use this for initialization
void Start () {
Debug.Log(noiseType[0]);
}
// Update is called once per frame
void Update () {
}
}
Did you try to debug this? Does noiseType
really have 15 elements? Object on scene can have other array.
Answer by Nerevar · May 15, 2014 at 01:45 PM
Hello,
In c# you can't assign/allocate memory when you declare you variables of a class (Unlike JS) or maybe it is unity specific... You have to do it in a Start(), initiation or constructor function :)
try:
using UnityEngine;
using System.Collections;
public class GodsToolGenerator : MonoBehaviour {
public int[] noiseType;
public float[] noiseInfluence;
public Noise noise;
// Use this for initialization
void Start () {
noiseType = new int[15];
noiseInfluence = new float[15];
noise = new Noise();
Debug.Log(noiseType[0]);
}
// Update is called once per frame
void Update () {
}
}
This is not true. This is probably mix up with constants
in C#.
Well I never call "new" when declaring variables of a class because it has never worked for me in c#, at least when you do that in start() it is working :3
Answer by aakwewaanaqa · Apr 09, 2017 at 04:46 PM
@0V3RR1D3 Hey I have find out the answer !!
try those in OnEnable(), and add [ExecuteInEditMode] attributes. Like this. :D
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class GodsToolGenerator : MonoBehaviour
{
public int[] noiseType = new int[15];
public float[] noiseInfluence = new float[15];
public Noise noise = new Noise();
private void OnEnable()
{
noiseType = new int[15];
noiseInfluence = new float[15];
Noise noise = new Noise();
}
}
This makes not much sense. OnEnable is called everytime the object is enabled. So every time your array would be overwritten.
The actual problem was that the array variable is declared as public and therefore serialized. That's why the field initializer doesn't work. If the variable is not serialized the field initializer does work. However the point of this question was to edit those values in the editor, so they need to be serialized. If a certain array length is required, it should be ensured in the custom inspector.