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()

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 :slight_smile:

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 () {
 
    }
}

@0V3RR1D3
Hey I have find out the answer !!

try those in OnEnable(), and add [ExecuteInEditMode] attributes.
Like this. :smiley:

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();
    }
}