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 neilvaidya95 · Jan 08, 2020 at 10:50 PM · beginnerc# tutorialstate machinedesign-patterns

NullReferenceException while following Unity3d College tutorial @JasonWeimann

I have followed the following tutorial as the setup for my state machine https://unity3d.college/2017/05/26/unity3d-design-patterns-state-basic-state-machine/

there is a null reference exception but I don't get why. Rigidbody, InputHandler and Character components all are attached to my character in the inspector. The RigidBody is attached to the Input handler and the Character class. Inputhandler does all the if(Input.GetButtonDown("blah blah")) stuff and returns some bools, it also checks for if grounded Inputhandler.grounded_ is a simple

 h_vel = Rb.velocity.x;
 grounded_ = (h_vel == 0f)? true : false;

and I have instantiated the rigidbody as Rb in InputHandler correctly

console is saying the issue is at Update() in character, and Tick() in Begin State. Please note at this point there are no forces being applied to the character so I'm considering the possibility that its not getting a value out of the rigid body, but I'm really not sure. Please help!

  public class Character : MonoBehaviour
     {
         public State currentState;
         public Rigidbody rb;
         public Animator animator;
     
         // Start is called before the first frame update
         private void Start()
         {
             SetState(new BeginState(this));
         }
     
         // Update is called once per frame
         private void Update()
         {
             currentState.Tick();
         }
         public void SetState(State state)
         {
             if (currentState != null)
             {
                 currentState.OnStateExit();
             }
     
             currentState = state;
     
             if (currentState != null)
             {
                 currentState.OnStateEnter();
             }
         }
     }
 
 
 ----------
   public abstract class State
     {
          protected Character character;
          protected float timescalar = 10f;
          protected Animator animator;
     
          public abstract void Tick();
          public virtual void OnStateEnter(){}
          public virtual void OnStateExit(){}
              public State(Character character)
         {
             this.character = character;
         }
     }
     
  
 
 


 public class BeginState : State
 {
     private InputHandler inputHandler;
     public BeginState(Character character) : base(character) { }
 
     public override void Tick()
     {
         if(inputHandler.grounded_)
         {
             character.SetState(new IdleState(character));
         }
         else
         {
             character.SetState(new AerialIdleState(character));
         }
     }
 
     public override void OnStateEnter()
     {
 
     }
 }
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

· Add your reply
  • Sort: 

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

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

Trying to understand the State Pattern - having trouble implementing 0 Answers

State Machine Tutorial Question 0 Answers

White line behind Object triggering collisions 0 Answers

Player animation FSM stopped working 0 Answers

Unity editor Slowness when starting 0 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