This question was
closed Aug 07, 2017 at 01:02 AM by
22ecvvw.
Question by
22ecvvw · Aug 06, 2017 at 08:24 PM ·
errornullreferenceexceptionsyntax-error
NullReferenceException: Object reference not set to an instance of an object
NullReferenceException: Object reference not set to an instance of an object LampStateMachine.YellowState () (at Assets/Scripts/LampStateMachine.cs:56) LampStateMachine.Update () (at Assets/Scripts/LampStateMachine.cs:32)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lamp : MonoBehaviour {
[SerializeField] private float greenLight = 5.0f;
[SerializeField] private float yellowLight = 5.0f;
[SerializeField] private float redLight = 5.0f;
private float timePassed;
void Start()
{
ResetTimeSinceLastTransition();
GetComponent<Renderer>().material.color = Color.gray;
}
void Update()
{
timePassed += Time.deltaTime;
}
public float GetTime()
{
return timePassed;
}
public void ResetTimeSinceLastTransition()
{
timePassed = 5.0f;
}
public float GetRedTime()
{
return redLight;
}
public float GetYellowTime()
{
return yellowLight;
}
public float GetGreenTime()
{
return greenLight;
}
}
This is my state machine, that holds the error. I can't seem to figure out how to fix this any ideas? Also the first script is placed on the lamp it self, the second script is placed on a empty game object.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LampStateMachine : MonoBehaviour {
private Lamp target;
private TrafficStates curState = TrafficStates.redLight;
private Dictionary<TrafficStates, Action> fsm = new Dictionary<TrafficStates, Action>();
void Start()
{
GetComponent<Lamp>();
fsm.Add(TrafficStates.redLight, new Action(RedState));
fsm.Add(TrafficStates.yellowLight, new Action(YellowState));
fsm.Add(TrafficStates.greenLight, new Action(GreenState));
SetState(TrafficStates.redLight);
SetState(TrafficStates.greenLight);
SetState(TrafficStates.yellowLight);
}
void Update()
{
fsm[curState].Invoke();
}
enum TrafficStates
{
greenLight,
yellowLight,
redLight
}
void RedState()
{
if (target.GetRedTime() > target.GetTime())
{
GameObject yellowObj = GameObject.Find("YellowLight");
yellowObj.GetComponent<Renderer>().material.color = Color.yellow;
SetState(TrafficStates.yellowLight);
}
}
void YellowState()
{
if (target.GetYellowTime() > target.GetTime())
{
GameObject greenObj = GameObject.Find("GreenLight");
greenObj.GetComponent<Renderer>().material.color = Color.green;
SetState(TrafficStates.greenLight);
}
}
void BlinkingYellowState()
{
}
void GreenState()
{
if (target.GetGreenTime() > target.GetTime())
{
GameObject redObj = GameObject.Find("RedLight");
redObj.GetComponent<Renderer>().material.color = Color.red;
SetState(TrafficStates.redLight);
}
}
void SetState(TrafficStates newState)
{
curState = newState;
}
}
Comment