1

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

firala
  • 67
  • 5
  • 4
    You never initialize `menu_items`. Where do you set `menu_items = new List – René Vogt Mar 17 '17 at 13:09
  • 2
    @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 Mar 17 '17 at 13:51
  • @rutter ok, it seems I don't know enough about unity, so I reopen the question. – René Vogt Mar 17 '17 at 13:53
  • 1
    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 Mar 17 '17 at 14:05

3 Answers3

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

Artur Nista
  • 359
  • 1
  • 4
  • 1
    In a Unity-based project, that may be ill-advised. Before assigning `menu_items`, a programmer may want to check if it was populated using values from Unity's serialization system. – rutter Mar 17 '17 at 13:49
  • Yes, that's true. My answer was directed to this case. A simple test like ```if(menu_items == null)``` should do the job. – Artur Nista Mar 17 '17 at 18:51
  • Oh man... thank you. I am still confused as to why Unity crashes instead of doing what it always does when I forget to initialize stuff - just sending me an error message. Edit: Okay, I changed it. Unity still crashes at the same point. – firala Mar 17 '17 at 19:33
1

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.

firala
  • 67
  • 5
1

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

OleMunz
  • 11
  • 2