Having a issue with a NullReferenceException
Hello, Im somewhat new to c# I'm working on programming a traffic light when a NullReferenceException is blocking me.
public class TrafficLight : MonoBehaviour {
[SerializeField]private float greenLight = 5.0f;
[SerializeField]private float yellowLight = 5.0f;
[SerializeField]private float redLight = 5.0f;
public GameObject CurObject;
private float timePassed;
void Start()
{
ResetTimeSinceLastRestart ();
GetComponent<Renderer> ().material.color = Color.gray;
}
void Update()
{
timePassed += Time.deltaTime;
}
public float GetTime()
{
return timePassed;
}
public void ResetTimeSinceLastRestart()
{
timePassed = 5.0f;
}
public float GetRedTime()
{
return redLight;
}
public float GetYellowTime()
{
return yellowLight;
}
public float GetGreenTime()
{
return greenLight;
}
}
and my StateMachine Script public class TrafficLightStateMachine : MonoBehaviour {
private TrafficLight target;
private TrafficStates curState = TrafficStates.redLight;
private Dictionary<TrafficStates, Action> fsm = new Dictionary<TrafficStates, Action>();
enum TrafficStates
{
greenLight,
yellowLight,
redLight
}
void Start ()
{
//this accesses the Data Model Script
GetComponent<TrafficLight> ();
//this is my dictionary
fsm.Add (TrafficStates.redLight, new Action (RedState));
fsm.Add (TrafficStates.yellowLight, new Action (YellowState));
fsm.Add (TrafficStates.greenLight, new Action (GreenState));
//This is the default state.
SetState (TrafficStates.redLight);
}
void Update ()
{
fsm [curState].Invoke();
}
//If the time is greater than timepassed it will transition to the next function: the Yellow light.
void RedState()
{
if (target.GetRedTime() > target.GetTime())
{
GameObject greenObj = GameObject.Find("Green Light");
greenObj.GetComponent<Renderer>().material.color = Color.green;
SetState (TrafficStates.greenLight);
}
}
//only pay attention to RedState, if i figure out how to fix the red state the rest should follow.
void YellowState()
{
if (target.GetYellowTime() > target.GetTime())
{
GameObject RedObj = GameObject.Find("Red Light");
RedObj.GetComponent<Renderer> ().material.color = Color.red;
SetState (TrafficStates.redLight);
}
}
void BlinkingYellowState()
{
}
void GreenState()
{
if (target.GetGreenTime() > target.GetTime())
{
GameObject yellowObj = GameObject.Find("Yellow Light");
yellowObj.GetComponent<Renderer> ().material.color = Color.yellow;
SetState (TrafficStates.yellowLight);
}
}
void SetState(TrafficStates newState)
{
curState = newState;
}
}
the errors appear on these two lines: NullReferenceException: Object reference not set to an instance of an object TrafficLightStateMachine.RedState () (at Assets/Lab 1_TrafficLight/Scripts/TrafficLightStateMachine.cs:40) TrafficLightStateMachine.Update () (at Assets/Lab 1_ Traffic Light/Scripts/TrafficLightStateMachine.cs:33)
I'm not sure what the error is and I've been trying for hours.
Answer by Positive7 · Oct 01, 2017 at 12:32 AM
You didn't initialize target
void Start ()
{
//this accesses the Data Model Script
GetComponent<TrafficLight> ();
//this is my dictionary
.......
it should be
void Start ()
{
//this accesses the Data Model Script
target = GetComponent<TrafficLight> ();
//this is my dictionary
.......
Your answer
Follow this Question
Related Questions
NullReferenceException when attempting to call a GameObject from within PointerEventData 1 Answer
(C#) Invoke giving null reference exception, crashing unity 0 Answers
Null Reference exception with custom class object 0 Answers
Null Reference Exception Error 1 Answer
Faulty NullReferenceException? 2 Answers