- Home /
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);
}
}
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);
}
}
veryvery thank you, i want to do the scene like angrybird but have no idea
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.
Answer by tw1st3d · Jul 24, 2013 at 03:55 AM
var m_bActive : boolean[] = new boolean[10];
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
Follow this Question
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