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 /
This question was closed Aug 07, 2017 at 01:02 AM by 22ecvvw.
avatar image
0
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
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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

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

Null Reference in script after having called other class without a null reference 0 Answers

NullReferenceException: Object reference not set to an instance of an object ... Why? 1 Answer

Missing ',' when there is no need? 0 Answers

Null Reference Exception Error 1 Answer

Why am I getting a null reference error? 1 Answer


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