The question is answered, right answer was accepted
How to add GameObjects to a list? Start can not take parameters?
hello,
I'm writing a script where a menu UI appears when you hit tab, and then cycles through several other menus as you press A and D. It does this by making a list of the UI menu gameobjects, and making the object at the current index active, and the others inactive. However, it is throwing two errors: the first is (GameMenus):Start(); can not take parameters. The other is ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection, whenever I press A or D. Clearly something is wrong with the start function but I'm failing to figure out what.
public GameObject characterMenu;
public GameObject Menu1;
public GameObject Menu2;
public GameObject Menu3;
public GameObject Menu4;
private int timeToggle;
public int currentIndex = 0;
public IList<GameObject> characterPageList = new List<GameObject>();
public void Start()
{
characterPageList.Add(Menu1);
characterPageList.Add(Menu2);
characterPageList.Add(Menu3);
characterPageList.Add(Menu4);
}
public void toggleCharacterMenu()
{
characterMenu.gameObject.SetActive(!characterMenu.gameObject.activeSelf);
timeToggle = Convert.ToInt32(!characterMenu.gameObject.activeSelf);
Time.timeScale = timeToggle;
Debug.Log("time is currently" + timeToggle);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Tab))
{
toggleCharacterMenu();
}
if (characterMenu.activeInHierarchy == true)
{
if (Input.GetKeyDown(KeyCode.D))
{
if (currentIndex + 1 > 3)
{
currentIndex = 0;
}
else
{
currentIndex = currentIndex + 1;
}
characterPageList[0 - 3].gameObject.SetActive(false);
characterPageList[currentIndex].gameObject.SetActive(true);
Debug.Log("current index is " + currentIndex);
}
if (Input.GetKeyDown(KeyCode.A))
{
if (currentIndex - 1 < 0)
{
currentIndex = 3;
}
else
{
currentIndex = currentIndex - 1;
}
characterPageList[currentIndex].gameObject.SetActive(true);
Debug.Log("current index is " + currentIndex);
}
Answer by HM_0000000 · Jul 25, 2020 at 04:12 AM
a;dfhaskf;eka I solved this like15 minutes after asking it, and after almost 4 hours of debugging. life is pain. The code inside the start function was moved to its own function, and then I called that new function inside the toggleCharacterMenu function, so that it just runs once when you open the menu.