Question by
jhartman1 · Oct 02, 2017 at 12:06 AM ·
c#timerstate-machinetimer countdownstate machine
Traffic Light won't change colors State machine
Hey guys, I recently posted a question about the same set of scripts when I had a error, now i can't seem to get the lights to turn on let alone change. I'm sure it's a simple fix but I can't figure it out. My traffic light script
[SerializeField]private float greenLight = 5.0f;
[SerializeField]private float yellowLight = 5.0f;
[SerializeField]private float redLight = 5.0f;
public GameObject redObj;
public GameObject yellowObj;
public GameObject greenObj;
private float timePassed;
void Start()
{
ResetTimeSinceLastRestart ();
GetComponent<Renderer> ().material.color = Color.grey;
greenObj.GetComponent<Renderer> ().material.color = Color.grey;
redObj.GetComponent<Renderer> ().material.color = Color.grey;
yellowObj.GetComponent<Renderer> ().material.color = Color.grey;
}
void Update()
{
timePassed += Time.deltaTime;
}
public float GetTime()
{
Debug.Log ("Prob1a"); // the problem is here
return timePassed;
}
public void ResetTimeSinceLastRestart()
{
timePassed = 5.0f;
}
public float GetRedTime()
{
return redLight;
}
public float GetYellowTime()
{
return yellowLight;
}
public float GetGreenTime()
{
return greenLight;
}
My TrafficLightStateMachine script ( if it's needed)
private TrafficLight target;
private TrafficStates curState = TrafficStates.redLight;
private Dictionary<TrafficStates, Action> fsm = new Dictionary<TrafficStates, Action>();
enum TrafficStates
{
greenLight,
yellowLight,
redLight
}
void Start ()
{
Debug.Log ("Prob0");
//this accesses the Data Model Script
target = 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));
// 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()
{
Debug.Log ("Prob1b");//this is the effected code, once this is fixed I can fix the others
if (target.GetRedTime() >= target.GetTime())
{
target.greenObj = GameObject.Find("Green Light");
target.greenObj.GetComponent<Renderer>().material.color = Color.green;
SetState (TrafficStates.greenLight);
}
}
//only pay attention to RedState cause if I can get this right the rest will be easy.
void YellowState()
{
if (target.GetYellowTime() > target.GetTime())
{
target.redObj = GameObject.Find("Red Light");
target.redObj.GetComponent<Renderer> ().material.color = Color.red;
SetState (TrafficStates.redLight);
}
}
void BlinkingYellowState()
{
}
void GreenState()
{
Debug.Log ("Prob3");
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;
}
Comment
Your answer
Follow this Question
Related Questions
Countdown timer... 2 Answers
Remove unnecessary text from a string 1 Answer
State Machine Tutorial Question 0 Answers
Clock doesn't stop when reaches 0.0. Can you help me with this. 1 Answer
Getting a timer to go to 0 1 Answer