Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by jhartman1 · Sep 30, 2017 at 07:53 PM · c#nullreferenceexceptionnull reference exceptionstate machine

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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
     .......


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jhartman1 · Oct 01, 2017 at 02:45 AM 0
Share

thanks that worked!,

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

115 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges