Search Unity

Resolved Null Reference Exception on an Integer?

Discussion in 'Scripting' started by frankiwinnie, Jun 12, 2022.

  1. frankiwinnie

    frankiwinnie

    Joined:
    Dec 1, 2020
    Posts:
    26
    Hello everyone,

    I am having a wierd problem that I cannot figure out why it happens. I have a function which runs when a UI Button is clicked. I have a list and I want to select a random game object from that list when the button is clicked.

    Code (CSharp):
    1. public void ExecuteOptionOne()
    2. {
    3. int x = Random.Range(0, myList.Count);
    4. GameObject gameObject = myList[x];}
    Somehow I am having a null reference exception error on the integer assigning line (x), don't know why?

    Any suggestions or resolutions on that problem? Thank you.
     
  2. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    Probably myList is null.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752