(Answered in edit, thank you everyone!)
My MenuScript in Unity should basically just create menu items on a public function call. However, on pressing Start in the editor, the whole program stops working (no error message, but nothing will ever happen).
I have narrowed it down to the list of dummy buttons existing (they will change according to the newMenu function). When accessing the menu_items variable inside the start function, all's fine. However, outside of it, Unity will proceed to crash.
public class MenuScript : MonoBehaviour {
public List<Button> menu_items;
public bool paused;
private bool gameStarted;
// Use this for initialization
void Start()
{
// initializing (previously forgotten, added upon comments!
menu_items = new List<Button>();
foreach (Button btn in GetComponentsInChildren<Button>()) {
menu_items.Add(btn);
}
print("start function test: " + menu_items[0].name); // works.
}
// Update is called once per frame
void Update () {
}
public bool newMenu(string item0, string item1, string item2, string item3)
{
int tmp = 0;
print("new Menu function test: " +menu_items[0].name); // CRASH
//if (createNewMenuItem(0, item0)) tmp++;
//if (createNewMenuItem(1, item1)) tmp++;
//if (createNewMenuItem(2, item2)) tmp++;
//if (createNewMenuItem(3, item3)) tmp++;
if (tmp == 4) return true;
else return false;
}
public void newMenu(int pos, string item)
{
print("newMenu function");
createNewMenuItem(pos, item);
}
private bool createNewMenuItem(int pos, string item)
{
print("Creating new menu item for position " + pos + ": " + item);
//Crashes unity:
//Text text = menu_items[pos].GetComponentInChildren<Text>();
//Text text = menu_items[pos].transform.GetChild(0).GetComponent<Text>();
/*
do button stuff here...
*/
return true;
}
// more unimportant stuff ...
}
Now, can anyone tell me what I am overlooking? I am getting very confused by this whole ordeal. Thanks!
Edit: Added initialization of menu_items. Does not fix problem though.
Edit2: So I got it to work by rewriting a lot. The problem - as I see it - is that Unity instead of as usual writing Error messages to my stupid spelling errors, it just crashes. I believe something for some reason is wrong with Unity's error system, though I can't really prove it. Thank you very much.