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

     }
 }

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

var m_bActive : boolean = new boolean[10];