Search Unity

Help... again...

Discussion in 'Scripting' started by Volt777, Feb 27, 2019.

  1. Volt777

    Volt777

    Joined:
    Aug 6, 2018
    Posts:
    16
    "NullReferenceException: Object reference not set to an instance of an object
    TalkyTalk.Update () (at Assets/TalkyTalk.cs:34)" pops up when I run this.
    Code (CSharp):
    1. public Dropdown micDD;
    2.  
    3. private void Update()
    4.     {
    5.         List<string> microphones = new List<string>() {};
    6.  
    7.         foreach(string device in Microphone.devices)
    8.         {
    9.             for(int i = 0; i < device.Length; i++)
    10.             {
    11.                 microphones.Add(device[i].ToString());
    12.             }
    13.            
    14.         }
    15.  
    16.         micDD.AddOptions(microphones);
    17.        
    18.     }
    except no one will actually answer my plead for help
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,175
    Before I touch on anything code-related, let's focus on this problem right here. First, learn to properly name your threads and don't just go with some combination of "I need help". Threads that are poorly titled are often deliberately ignored. Just as an example this thread could have been "null reference exception".

    Second, responses take time. For the most part the answers you get are coming from volunteers and wait times can vary anywhere from minutes to hours depending on who is online. I checked your other threads and, aside from similarly bad title names, responses to them were very quick.

    With that out of the way, let's start by talking about error messages. If you look at the error message you'll notice that there is a file name followed by a semi-colon followed by a number. That number is the line the error occurs on.

    You've only posted part of a script and while you did post the correct function that the error is in the reality is we're going to have to guess where the error is because we don't know which line is line 34. In the future I recommend posting the entire script or if it's too long to post (ie thousands of lines) telling us which line in the pasted code is the one the error occurred on.

    Like I said I don't know which line the error is on, but it's a safe bet that you need to verify that micDD is correctly holding a reference to the Dropdown UI. Forgetting to assign references to scripts in the Inspector is a very common cause of null reference exceptions.

    Code (CSharp):
    1. public Dropdown micDD;
    2.  
    3. private void Update()
    4.     {
    5.         List<string> microphones = new List<string>() {};
    6.  
    7.         foreach(string device in Microphone.devices)
    8.         {
    9.             for(int i = 0; i < device.Length; i++)
    10.             {
    11.                 microphones.Add(device[i].ToString());
    12.             }
    13.  
    14.         }
    15.  
    16.         micDD.AddOptions(microphones);
    17.  
    18.     }
     
    Last edited: Feb 27, 2019
    Brathnann, Munchy2007 and Kurt-Dekker like this.