# Problems with arrays

I'm having a problem with the arrays that is in my code, I found the problem,but I don't know where or what lines to fix it. First, I'm setting variables and I dragged the gameobjects and buttons inside below arrays in the inspector

public GameObject[] expansion;
public Button[] panelButton;
public int isExpanded = 0;


Then I call the gameobjects inside the arrays using these function by button that I've created in the scene.

public void Expand()
{
Debug.Log(isExpanded);
while(isExpanded>0)
{
if(expansion[isExpanded] != null && panelButton[isExpanded] != null)
{
expansion[isExpanded].gameObject.SetActive(true);
panelButton[isExpanded].gameObject.SetActive(true);
Debug.Log(panelButton[isExpanded]);
isExpanded++;
return;
}
}
}


I also created this function to enable and disable the gameobjects inside the array using panelButton that is visible in the scene

public void SelectBase(GameObject selectBase)
{
if(expansion[isExpanded] != null && panelButton[isExpanded] != null)
{
if(selectBase == expansion[isExpanded])
{
return;
}
if(expansion[isExpanded] != null)
{
expansion[isExpanded].gameObject.GetComponent<BoxCollider>().enabled = false;
expansion[isExpanded].gameObject.tag = "Base";
}
if(selectBase !=null)
{
selectBase.gameObject.GetComponent<BoxCollider>().enabled = true;
selectBase.gameObject.tag = "NewBase";
}
expansion[isExpanded] = selectBase;
}
return;
}


The problem is, when I click on the SelectBase function, it will affecting the isExpanded value thus messing up with the gameobject arrays. Can anyone possibly give me some insights? Thank you

• This line while(isExpanded>0) seems pretty equivalent to while(true) since you're using isExpanded as an array element indicator, I'm assuming it's either zero or higher. With that in mind, if either expansion[isExpanded] or panelButton[isExpanded] is ever null, you're never getting out of that loop. Also, it could be helpful to include what happens when you try to run the code in its current state. – John Hamilton May 23 '17 at 7:25
• Thanks for the reply, and yes the isExpanded value is set to 1 in the inspector, since I set one of the expansion gameobject in the array will be visible in the scene when the game starts. When I run the scene, if I click, let say a button containing SelectBase(), it will work once, but after I click on the button containing Expand(), the panelButton will be visible, but not the expansion gameobject, and the SelectBase() will increase the inExpanded value too. – user3132527 May 23 '17 at 7:50
• I take it your loop isn't in a coroutine, meaning it will freeze your game forever when it runs until it crashes due to an OutOfBoundsException, at which point Unity will disable the script and all your code will stop working. – DisturbedNeo May 23 '17 at 15:51
• I finally fixed the problem but I have a problem when the condition is met inside one of the functions. I already updated the code.. – user3132527 May 24 '17 at 3:41
• So you fixed the original problem, and updated it with a completely different problem? – Gnemlock May 24 '17 at 3:47

Ok so I managed to fixed the arrays. I messed up with the isExpanded count by the way, and that's why the functions weren't working properly in the first place. I also removed the while loop.

void Expand()
{
if(isExpanded<=2)
{
Debug.Log(isExpanded);
isExpanded=isExpanded+1;
expansion[isExpanded].gameObject.SetActive(true);
panelButton[isExpanded].gameObject.SetActive(true);
Debug.Log(panelButton[isExpanded]);
Debug.Log(expansion[isExpanded]);
Debug.Log(isExpanded);
}
else if(isExpanded>2)
return;
Debug.Log("all spawned");
}

void Remove()
{
Debug.Log(isExpanded);
if(isExpanded>0)
{
expansion[isExpanded].gameObject.SetActive(false);
panelButton[isExpanded].gameObject.SetActive(false);
isExpanded=isExpanded-1;
Debug.Log(isExpanded);
}
else if(isExpanded==0)
return;
Debug.Log("done");
}


But I still have to deal with the 'IndexOutOfRangeException: Array index is out of range.' everytime I click the button that runs Expand(), and all the objects inside the array is visible though, while the new Remove() function that I've made is working properly.