Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Unity 2018.3 is now released.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Accessing light object in a script on same object

Discussion in 'Scripting' started by unity_aB0ViU4X_pf-Cg, Feb 11, 2019.

  1. unity_aB0ViU4X_pf-Cg

    unity_aB0ViU4X_pf-Cg

    Joined:
    Feb 11, 2019
    Posts:
    2
    Hi, I don't post here very often, but I have a question about a side project I am working on. I am creating a traffic light control program with Unity 2018 C#. I created a couple functions to turn the traffic lights on and off. If I include the functions in every script that is attached to a traffic light the functions work properly. If I move the functions off the script and place them in a script by themselves but still on the traffic light I get null reference exception even though I have reference the script.

    The first 2 scripts are the way I want to setup the scripts but can't get them to work.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class SouthLeftTurnLane : MonoBehaviour
    4. {
    5.     private RandomTraffic randomTraffic;
    6.     private TrafficLightColors color;
    7.     private float timer;
    8.     public int sequence;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         timer = 0;
    14.         randomTraffic = FindObjectOfType<RandomTraffic>();
    15.         color = FindObjectOfType<TrafficLightColors>();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (timer <= 0)
    22.         {
    23.             // Reset timer
    24.             timer = randomTraffic.SouthLeftTurnLight();
    25.             if (timer > 1 && timer < 5)
    26.             {
    27.                 timer = timer + 5;
    28.             }
    29.         }
    30.         Timer();
    31.         if (timer > 5)
    32.         {
    33.             //color.TurnGreenLightOn();
    34.             sequence = 1;
    35.         }
    36.         if (timer > 2 && timer < 5)
    37.         {
    38.             //color.TurnYellowLightOn();
    39.             sequence = 2;
    40.         }
    41.         if (timer < 2)
    42.         {
    43.             //color.TurnRedLightOn();
    44.             sequence = 3;
    45.         }
    46.         Debug.Log(GetSequence());
    47.     }
    48.     void Timer()
    49.     {
    50.         timer -= Time.deltaTime;
    51.     }
    52.  
    53.     public int GetSequence()
    54.     {
    55.         return sequence;
    56.     }
    57. }
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TrafficLightColors : MonoBehaviour
    4. {
    5.     public Light red;
    6.     public Light yellow;
    7.     public Light green;
    8.  
    9.     public GameObject trafficLight;
    10.  
    11.     private Light redLight;
    12.     private Light yellowLight;
    13.     private Light greenLight;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         redLight = red.GetComponent<Light>();
    19.         yellowLight = yellow.GetComponent<Light>();
    20.         greenLight = green.GetComponent<Light>();
    21.     }
    22.    
    23.     public void TurnGreenLightOn()
    24.     {
    25.         redLight.enabled = false;
    26.         yellowLight.enabled = false;
    27.         greenLight.enabled = true;
    28.     }
    29.     public void TurnYellowLightOn()
    30.     {
    31.         redLight.enabled = false;
    32.         yellowLight.enabled = true;
    33.         greenLight.enabled = false;
    34.     }
    35.  
    36.     public void TurnRedLightOn()
    37.     {
    38.         redLight.enabled = true;
    39.         yellowLight.enabled = false;
    40.         greenLight.enabled = false;
    41.     }
    42. }
    The last script includes the functions instead of putting them in a separate file like I would like, but I have no idea why they work here and not the in the first 2 scripts above. I am just trying to clean up the code and put the functions in a separate file.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class NorthLeftTurnLane : MonoBehaviour
    4. {
    5.     private RandomTraffic randomTraffic;
    6.    
    7.     public Light redLight;
    8.     public Light yellowLight;
    9.     public Light greenLight;
    10.  
    11.     private Light currentRedLight;
    12.     private Light currentYellowLight;
    13.     private Light currentGreenLight;
    14.  
    15.     private float timer;
    16.     public int sequence;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         timer = 0;
    22.         randomTraffic = FindObjectOfType<RandomTraffic>();
    23.         currentRedLight = redLight.GetComponent<Light>();
    24.         currentYellowLight = yellowLight.GetComponent<Light>();
    25.         currentGreenLight = greenLight.GetComponent<Light>();
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.         if (timer <= 0)
    32.         {
    33.             // Reset timer
    34.             timer = randomTraffic.NorthLeftTurnLight();
    35.             if (timer > 1 && timer < 5)
    36.             {
    37.                 timer = timer + 5;
    38.             }
    39.         }
    40.         Timer();
    41.         if (timer > 5)
    42.         {
    43.             GreenLight();
    44.             sequence = 1;
    45.         }
    46.         if (timer > 2 && timer < 5)
    47.         {
    48.             YellowLight();
    49.             sequence = 2;
    50.         }
    51.         if (timer < 2)
    52.         {
    53.             RedLight();
    54.             sequence = 3;
    55.         }
    56.         Debug.Log(GetSequence());
    57.     }
    58.     void Timer()
    59.     {
    60.         timer -= Time.deltaTime;
    61.     }
    62.  
    63.     public int GetSequence()
    64.     {
    65.         return sequence;
    66.     }
    67.  
    68.     void RedLight()
    69.     {
    70.         currentRedLight.enabled = true;
    71.         currentYellowLight.enabled = false;
    72.         currentGreenLight.enabled = false;
    73.     }
    74.     void YellowLight()
    75.     {
    76.         currentRedLight.enabled = false;
    77.         currentYellowLight.enabled = true;
    78.         currentGreenLight.enabled = false;
    79.     }
    80.     void GreenLight()
    81.     {
    82.         currentRedLight.enabled = false;
    83.         currentYellowLight.enabled = false;
    84.         currentGreenLight.enabled = true;
    85.     }
    86. }
    Thanks in advance for any help!
     
  2. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    383
    Which script causes the NULL reference exception, and on what line number?

    What you're doing here doesn't make much sense.
    Code (CSharp):
    1.  
    2.     // TrafficLightColors
    3.     public Light red;
    4.     public Light yellow;
    5.     public Light green;
    6.     public GameObject trafficLight;
    7.     private Light redLight;
    8.     private Light yellowLight;
    9.     private Light greenLight;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         redLight = red.GetComponent<Light>();
    14.         yellowLight = yellow.GetComponent<Light>();
    15.         greenLight = green.GetComponent<Light>();
    16.     }
    You can completely remove the Start function, since you've already assigned the lights in the inspector!
     
  3. unity_aB0ViU4X_pf-Cg

    unity_aB0ViU4X_pf-Cg

    Joined:
    Feb 11, 2019
    Posts:
    2
    I see what I was doing wrong and removed the start and private variables and can access the public variables already assigned in the inspector. I found out I have been programming around a much bigger issue that has to do with the prefab lights that I am using that I made in the unity editor. I have to work on or redesign my prefab before I can go any further. I will post a thread in the prefabs section if I can't get this figured out. Thanks for the scripting help though!
     
    Magiichan likes this.