Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by 0V3RR1D3 · May 15, 2014 at 12:03 PM · c#customizationcustom-editorindexoutofrangeexception

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 0V3RR1D3 · May 15, 2014 at 12:19 PM 0
Share

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 () {
     
     }
 }
 
avatar image Andrey-Postelzhuk · May 15, 2014 at 01:32 PM 0
Share

Did you try to debug this? Does noiseType really have 15 elements? Object on scene can have other array.

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

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 () {
  
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Andrey-Postelzhuk · May 15, 2014 at 02:20 PM 0
Share

This is not true. This is probably mix up with constants in C#.

avatar image Nerevar · May 15, 2014 at 02:38 PM 0
Share

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

avatar image
-1

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();
     }
 }
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Apr 09, 2017 at 05:51 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

View and Edit Properties of List items in inspector 1 Answer

Bug with adding clothes to character's body (twist mesh) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges