Creating array for cycling through 5 objects

Hey all. Here’s the code I’m working with:

using UnityEngine;
using System.Collections;

public class CycleObjects : MonoBehaviour {

public GameObject option1;
public GameObject option2;
public GameObject option3;
public GameObject option4;
public GameObject option5;

void Start(){
option1.SetActive (true);
option2.SetActive (false);
option3.SetActive (false);
option4.SetActive (false);
option5.SetActive (false);
}

// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.R)){
cycleobjects();
}
}

void cycleobjects ()
{
if (option1.activeSelf) {
option1.SetActive (false);
option2.SetActive (true);
option3.SetActive (false);
option4.SetActive (false);
option5.SetActive (false);
} else {
if (option2.activeSelf) {
option1.SetActive (false);
option2.SetActive (false);
option3.SetActive (true);
option4.SetActive (false);
option5.SetActive (false);
} else {
if (option3.activeSelf) {
option1.SetActive (false);
option2.SetActive (false);
option3.SetActive (false);
option4.SetActive (true);
option5.SetActive (false);
} else {
if (option4.activeSelf) {
option1.SetActive (false);
option2.SetActive (false);
option3.SetActive (false);
option4.SetActive (false);
option5.SetActive (true);
} else {
if (option5.activeSelf) {
option1.SetActive (true);
option2.SetActive (false);
option3.SetActive (false);
option4.SetActive (false);
option5.SetActive (false);
}
}
}
}
}
}
}

Of course, this isn’t optimized and would like to figure out a way to instantiate these instances within an array. Any/all help, tuts, feedback would be awesome. Still learning.

First, use code tags.

The basic syntax for an array serving this function would be something like:

public GameObject[] options;
public int currentCycle = 0;
void Start() {
Cycle(currentCycle);
}
void Update() {
if (Input.GetKeyDown(KeyCode.R)) {
currentCycle = (currentCycle + 1) % options.Length;
Cycle(currentCycle);
}
}
void Cycle(int input) {
for (int o=0; o < options.Length;o++) {
options[o].SetActive ( o == input);
}
}

Ok so a few things.

You could use an Array, which you already seem to know about. You would just declare your array like so:

GameObject [] myGameObjectArray =  new GameObject[length]();

//I do not recall if you need to put the length of the array in the variable declaration so try this if you get a syntax error:

GameObject [length] myGameObjectArray = new GameObject[length]();

However if you wanted to expand or shrink that array it would be very computationally expensive. So try using a List. They are part of the .NET library (I believe its in System.Generic) and you can just call myList.Add(myGameObject); to add it to it. You can access it just like an array, using the brackets like so: myList[ index ];
Lastly if you wanted to set all of your objects inactive at once you could child them beneath an empty game object and just call setActive(bool) on the parent. That will make all of them the same regardless, and you don’t need to monkey with any loops.

// instead of 5 unique variables*
public GameObject [] options;
// to keep track of which position / index 
int currCycle = 0;

// little updated logic to your cycle call.
if(Input.GetKeyDown(KeyCode.R)){
options[currCycle].setActive(false);
currCycle++;
options[currCycle].setActive(true);
if(currCycle == options.Length) currCycle = 0;