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 charlie8130 · Jul 24, 2013 at 03:11 AM · arrayindexoutofrangeexception

IndexOutOfRangeException: Array index is out of range.

i have a ploblem,the sentense m_bActive[idx] = true ; keep wrong

help me

 #pragma strict
 
 
 PlayerPrefs.SetInt("playerScore3", 70);
 
 var m_bActive : boolean[];
 
 var idx:int;
 
 var m_GuiStageBtn    : GUIStyle;
 
 var m_texLocked : Texture2D;
 
 var m_texActive  : Texture2D;
 
 var i :int;
 
 
 for (idx = 1; idx < 10; ++idx)
 
  if(PlayerPrefs.GetInt("playerScore3")>60)
       
     m_bActive[idx] = true ;
     
     else
     
      m_bActive[idx]  = false;
      
 m_bActive[0] = true;
 
 // col_1
 
 
 for ( i = 0; i < 9; i ++)
 {
      if (  m_bActive[idx])
 
          m_GuiStageBtn.normal.background = m_texActive;
 
      else
 
          m_GuiStageBtn.normal.background = m_texLocked;
 
                         
      if (GUI.Button(Rect(80, 30 * i + 220 , 20, 20), "", m_GuiStageBtn))
 
      {
         
     
    Application.LoadLevel(idx + 1);
 
      }
  }
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

2 Replies

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

Answer by clunk47 · Jul 24, 2013 at 03:28 AM

I'm not sure why you're not using any methods in your code at all. I added in the Update method because I don't know if you want to do this on every frame or on startup, and I added the GUI function because you cannot call a GUI button without GUI method... Anyway, you were getting the index out of range because you m_bActive boolean array has a length of 0 when this occurs. I'm sure you know to fill in your array in the inspector since the variable is public. Here is a way to be sure this code only fires if your array has at least one active element. Check the length of the array. If it is not greater than zero, the code won't fire and vice versa. I kept your GUI code, but commented out and used my own button just for testing, and another way of loading the next level, if this is what you wanted to do. Here is the error free code.

 #pragma strict
 
 PlayerPrefs.SetInt("playerScore3", 70);
 var m_bActive : boolean[];
 var idx:int;
 var m_GuiStageBtn : GUIStyle;
 var m_texLocked : Texture2D;
 var m_texActive : Texture2D;
 var i :int;
 
 function Update()
 {
     for (idx = 0; idx < 10; idx++)
     {
         if(m_bActive.Length > 0)
         {
             if(PlayerPrefs.GetInt("playerScore3")>60)
                 m_bActive[idx] = true ;
             else
             {
                 m_bActive[idx]  = false;
                 m_bActive[0] = true;
             }
             for ( i = 0; i < 9; i ++) 
             { 
                 if (m_bActive[idx])
                     m_GuiStageBtn.normal.background = m_texActive;
                 else
                     m_GuiStageBtn.normal.background = m_texLocked;
             }
         }
         
         else
             Debug.Log("m_bActive has a length of 0!");
     }
 }
 
 function OnGUI()
 {
     //if (GUI.Button(Rect(80, 30 * i + 220 , 20, 20), "", m_GuiStageBtn))
     if(GUI.Button(Rect(0, 0, 128, 64), "Next"))
     {
         if(Application.levelCount > Application.loadedLevel)
             Application.LoadLevel(Application.loadedLevel + 1);
     }
 }
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 charlie8130 · Jul 24, 2013 at 03:47 AM 0
Share

veryvery thank you, i want to do the scene like angrybird but have no ideaalt text

angry bird.jpg (156.3 kB)
avatar image clunk47 · Jul 24, 2013 at 03:54 AM 1
Share

If you were trying to have multiple buttons using an iteration, you'd need to use GUILayout. But the best way to do this, have a look at GUI.SelectionGrid. You can have a grid of buttons, set the margin, and give each button its own GUIContent. Study up on these two things and you'll achieve what you are wanting.

avatar image
2

Answer by tw1st3d · Jul 24, 2013 at 03:55 AM

 var m_bActive : boolean[] = new boolean[10];
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 clunk47 · Jul 24, 2013 at 04:06 AM 1
Share

This will work to populate via script rather than the inspector. Since this will get rid of the error he was getting, +1.

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

17 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

Related Questions

How to learn amount of objects in selected prefabs folder? 0 Answers

IndexOutOfRangeException: Array index is out of range when using an Array and instantiating 2 Answers

IndexOutOfRangeException: Array index is out of range. 0 Answers

Array index is out of range error Edited 1 Answer

FindGameobjectsWithTag -> Array 2 Answers


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