Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

NullReferenceException confusion

Discussion in 'Scripting' started by adam2050, Mar 10, 2015.

  1. adam2050

    adam2050

    Joined:
    Mar 8, 2015
    Posts:
    16
    Hi, first time posting on the forums. I know the NullReferenceException error is an oldie and I have google searched the forums but I as far I can tell I am intialising. I am creating a finite state machine and am creating a test environment for it by changing colours of spheres in a scene. Each sphere is correspodingly named to an emotion (same names as in the Emotions {}) . To avoid framerate issues I am finding GameObjects in the Start(), then I am initialising the object renderer and adding that to a render varible. I am then adding the render variable to a list. I have clearly misunderstood some aspect of the initialisation process as I get a NullReferenceException at line 20 when I try to add the RCalm to the list and would appreicate some guidance.

    I have also tried a simplified approach of RCalm =GameObject.find("Calm").GetCompenent<Renderer>(); and then adding to the list but I am still getting the same error.

    Many thanks


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. public enum Emotions { Calm, Fear, Worry, Panic, Resolve, Pain, Surpise, Confusion, Flight, Fight}
    5.  
    6.  
    7. public class EmotionTest : MonoBehaviour {
    8.  
    9.     public Emotions KrakenEmotions = Emotions.Calm;
    10.     public GameObject SCalm, SFear, SWorry, SPanic, SResolve,SPain, SSurprise, SConfusion, SFlight, SFight;
    11.     public Renderer  RCalm, RFear, RWorry, RPanic, RResolve,RPain, RSurprise,RConfusion, RFlight, RFight;
    12.     //private GameObject[] EmotionArray;
    13.     private List<GameObject> EmotionArray;
    14.     private List <Renderer> EmoRend;
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         SCalm = GameObject.Find ("Calm");
    19.         RCalm = SCalm.GetComponent<Renderer> ();
    20.         EmoRend.Add (RCalm);
    21.         SFear = GameObject.Find ("Fear");
    22.         SWorry = GameObject.Find ("Worry");
    23.         SPanic = GameObject.Find ("Panic");
    24.         SResolve = GameObject.Find ("Resolve");
    25.         SPain = GameObject.Find ("Pain");
    26.         SSurprise = GameObject.Find ("Surprise");
    27.         SConfusion = GameObject.Find ("Confusion");
    28.         SFlight = GameObject.Find ("Flight");
    29.         SFight = GameObject.Find ("Fight");
    30.    
    31.         //EmotionArray.Add (SCalm, SFear, SWorry, SPanic, SResolve, SSurprise, SConfusion, SFlight, SFight);
    32.  
    33.  
    34.  
    35.         /*
    36.         EmotionArray.Add (SCalm);
    37.  
    38.         EmotionArray.Add (SFear);
    39.         EmotionArray.Add (SWorry);
    40.         EmotionArray.Add (SPanic);
    41.         EmotionArray.Add (SResolve);
    42.         EmotionArray.Add (SPain);
    43.         EmotionArray.Add (SSurprise);
    44.         EmotionArray.Add (SConfusion);
    45.         EmotionArray.Add (SFlight);
    46.         EmotionArray.Add (SFight);
    47.  
    48. */
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    You need to allocate the lists first. Like this:
    Code (CSharp):
    1.     private List<GameObject> EmotionArray = new List<GameObject>();
    2.     private List <Renderer> EmoRend = new List<Renderer>();
     
  3. adam2050

    adam2050

    Joined:
    Mar 8, 2015
    Posts:
    16
    Thank you. Will try that.
     
  4. adam2050

    adam2050

    Joined:
    Mar 8, 2015
    Posts:
    16
    Error gone much appreicated