1
votes

(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.

3
You never initialize menu_items. Where do you set menu_items = new List<Button>()? - René Vogt
@RenéVogt I'm inclined to assume that menu_items is populated via Unity's serialization system. The behavior described above sounds more like boundless loop or recursion than a simple NRE, which should be logged without hanging Unity. - rutter
@rutter ok, it seems I don't know enough about unity, so I reopen the question. - René Vogt
No error message at all? Hmmm, that seems odd. Can you attach a debugger to it and just try stepping through the code, to see what happens? If it's a logical/looping error, it'll become apparent quickly enough. - Serlite

3 Answers

1
votes
void Start() {
  menu_items = new List<Button>();

  foreach (Button btn in GetComponentsInChildren<Button>()) { 
      menu_items.Add(btn);
  }
  print("start function test: " + menu_items[0].name); // works.

}  

You need to add the menu_items = new List(); to instantiate a new List object before using it.

1
votes

So I got it to work by rewriting the code - first to no avail, until I fixed some spelling errors that, from my experience, should actually just give me Exceptions and not crash the program. 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. I'll try to reinstall or something if the problem comes up again. Thank you very much.

1
votes

I had almost the same problem. Turned out there was a simple mistake with my spelling. Mixed up private fieldA vs public FieldA -> capital letters. I got no error message from Unity Eidtor. Editor froze every time and had to be shut down the hard way (killing the process).