Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by SharkoFR · Apr 09, 2017 at 05:43 PM · indexoutofrangeexception

IndexOutOfRangeException: Array index is out of range. SceneSwitcher.OnGUI () (at Assets/Packages/Curvy Examples/Scripts/SceneSwitcher.cs:125)

HI i have this error in the curvy plugin : IndexOutOfRangeException: Array index is out of range. SceneSwitcher.OnGUI () (at Assets/Packages/Curvy Examples/Scripts/SceneSwitcher.cs:125)

the complete code:

 #if UNITY_EDITOR
 using UnityEditor;
 #endif
 
 public class SceneSwitcher : MonoBehaviour {
 
     
     bool mChkAdditive;
     Vector2 sv = Vector2.zero;
     Rect mDropdownRect;
     string[] mItems;
     bool mShow;
 
 #if UNITY_EDITOR
     void Start ()
     {
         
     }
 #endif
 
     int CurrentLevel
     {
         get
         {
 #if UNITY_5_0 || UNITY_5_1 || UNITY_5_2
             return Application.loadedLevel;
 #else
             return UnityEngine.SceneManagement.SceneManager.GetActiveScene().buildIndex;
 #endif
         }
         set
         {
             if (CurrentLevel != value)
             {
 #if UNITY_5_0 || UNITY_5_1 || UNITY_5_2
                 if (mChkAdditive)
                     Application.LoadLevelAdditive(value);
                 else
                     Application.LoadLevel(value);
 #else
                 if (mChkAdditive)
                     UnityEngine.SceneManagement.SceneManager.LoadScene(value, UnityEngine.SceneManagement.LoadSceneMode.Additive);
                 else
                     UnityEngine.SceneManagement.SceneManager.LoadScene(value,UnityEngine.SceneManagement.LoadSceneMode.Single);
 #endif
             }
         }
     }
 
     void getScenes()
     {
         var items = new List<string>();
         foreach (var lvl in EditorBuildSettings.scenes)
         {
             if (lvl.enabled)
             {
                 var path = lvl.path.Split('/');
                 items.Add(path[path.Length - 1].TrimEnd(".unity"));
             }
         }
         mItems = items.ToArray();
     }
 
 
     void onSelect(object index)
     {
         CurrentLevel = (int)index;
     }
 
     void OnGUI()
     {
         if (mItems == null)
         {
             mDropdownRect = new Rect(Screen.width - 200, 0, 295, 300);
             getScenes();
         }
         
         if (mItems.Length == 0)
         {
             GUI.Label(new Rect(Screen.width-480, mDropdownRect.y, 460, 40),"Add scenes to the build settings to enable the scene switcher!");
         }
         else {
             mChkAdditive=GUI.Toggle(new Rect(mDropdownRect.x - 180, mDropdownRect.y, 160, 20), mChkAdditive, "Additive");
             if (GUI.Button(new Rect((mDropdownRect.x - 100), mDropdownRect.y, mDropdownRect.width, 25), ""))
             {
                 if (!mShow)
                 {
                     mShow = true;
                 }
                 else
                 {
                     mShow = false;
                 }
             }
 
             if (mShow)
             {
                 sv = GUI.BeginScrollView(new Rect((mDropdownRect.x - 100), (mDropdownRect.y + 25), mDropdownRect.width, mDropdownRect.height), sv, new Rect(0, 0, mDropdownRect.width, Mathf.Max(mDropdownRect.height, (mItems.Length * 25))));
 
                 GUI.Box(new Rect(0, 0, mDropdownRect.width, Mathf.Max(mDropdownRect.height, (mItems.Length * 25))), "");
 
                 for (int index = 0; index < mItems.Length; index++)
                 {
 
                     if (GUI.Button(new Rect(0, (index * 25), mDropdownRect.height, 25), ""))
                     {
                         mShow = false;
                         CurrentLevel = index;
                     }
 
                     GUI.Label(new Rect(5, (index * 25), mDropdownRect.height, 25), mItems[index]);
 
                 }
 
                 GUI.EndScrollView();
             }
             else
             {
                 GUI.Label(new Rect((mDropdownRect.x - 95), mDropdownRect.y, 300, 25), mItems[CurrentLevel]);
             }
         }
 
     }
 
 
 }
 

the part of the code who have a error :

             {
                 GUI.Label(new Rect((mDropdownRect.x - 95), mDropdownRect.y, 300, 25), mItems[CurrentLevel]);
             }

i'm on unity 5.6 (last version)

thanks you!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by toddisarockstar · Apr 09, 2017 at 09:35 PM

this error always means you are checking for a index number in an array higher that the length of the array.

in other words, the CurrentLevel variable is pry going higher that the length of the mItems array for whatever reason.

Comment
Add comment · 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

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

98 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

,"For" loop looping only once through my array 0 Answers

IndexOutOfRangeException: Array index is out of range. 2 Answers

C# List index confusion using Mathf.Clamp 1 Answer

C# Index Out of Range Exception When the Index Is Within the Length of the Array 0 Answers

IndexOutOfRangeException in Animation 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