Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by XQuirky_TurtleX · Jan 21, 2021 at 02:56 PM · buildinstancestate-machineobject reference

Object has no problem finding instance in editor. However in build it does.

Hi, hope u don't run into this problem.

I'm working on this project, and when I make a build, the movement stops working.
The movement is a basic AddForce(), but works in a State Machine architecture (since im a dumb dumb, I only started making test builds close to the "deadline").
After doing some debugging, I found out it is able to find the instance to Log in the console, but does not find it to check the bool. As u can see below:

  public override void Update(PlayerController player)
 {
     Debug.Log("PLAYER CONTROLLER: " + player);
     if (player._input.inputGiven)
     {
         Debug.Log("PLAYER CONTROLLER 2: " + player);
         player.TransitionToState(player.moveState);
     }
 }


I already tried lots of stuff, but without any reasonable progress.
Below u can see the console in editor, and in the build.

alt text

alt text



ALSO im not the best coder, so please, even tho I checked my code coutless times u can check it below, I choose only to show parts related to the issue for the readers sake.

Player Controller:

  public class PlayerController : MonoBehaviour
 {
     //--------------------STATE MACHINE------------------------------------
     PlayerBaseState currentState;

     public readonly PlayerIdleState idleState = new PlayerIdleState();      //
     public readonly PlayerMoveState moveState = new PlayerMoveState();      // SO PURFECT
     public readonly PlayerDeadState deadState = new PlayerDeadState();      //    W0W
     public readonly PlayerDashState dashState = new PlayerDashState();      //
     public readonly PlayerGrabState grabState = new PlayerGrabState();      //
     //--------------------STATE MACHINE------------------------------------

     [HideInInspector] public InputHandler _input;

(...)

     private void Awake()
     {
         _input = InputHandler.Instance;
         rigidbody = GetComponent<Rigidbody>();

         enemyLayer = LayerMask.GetMask("Enemies");
         minDistance = detectRange;

         currentHp = maxHp;

         engageDrag = rigidbody.drag;//               Set Moving Drag
         engageAngularDrag = rigidbody.angularDrag;//
         engageMass = rigidbody.mass;
     }

     void Start()
     {
         TransitionToState(idleState);//default state
     }

     void Update()
     {
         currentState.Update(this); 
     }

     private void OnCollisionEnter(Collision collision)
     {
         currentState.OnCollisionEnter(this);
     }

     private void FixedUpdate()
     {
         currentState.FixedUpdate(this);
     }

     public void TransitionToState (PlayerBaseState state)
     {
         currentState = state;
         currentState.EnterState(this);
     }

(...)
BaseState:

  public abstract class PlayerBaseState
 {
     public abstract void EnterState(PlayerController player);

     public abstract void Update(PlayerController player);

     public abstract void FixedUpdate(PlayerController player);

     public abstract void OnCollisionEnter(PlayerController player);
 }


Player Idle State:

  public class PlayerIdleState : PlayerBaseState
 {
     public override void EnterState(PlayerController player)
     {
     }

     public override void OnCollisionEnter(PlayerController player)
     {
 
     }

     public override void Update(PlayerController player)
     {
         Debug.Log("PLAYER CONTROLLER: " + player);
         if (player._input.inputGiven)
         {
             Debug.Log("INPUt 2: " + player);
             player.TransitionToState(player.moveState);
         }
     }

     public override void FixedUpdate(PlayerController player)
     {
         player.CheckForEnemies();
     }
 }


(...)
Thanks in advance :D

in-editor-error.png (413.6 kB)
error.jpg (396.4 kB)
Comment
Add comment · Show 2
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 jackmw94 · Jan 21, 2021 at 02:14 PM 1
Share

Can you post your InputHandler code? :)

avatar image XQuirky_TurtleX jackmw94 · Jan 21, 2021 at 02:56 PM 0
Share

Thanks for the help! It is unfinished but feel free to roast me :D

1 Reply

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

Answer by jackmw94 · Jan 21, 2021 at 03:21 PM

I have a feeling that this might be because the ordering of the 'magic functions' such as Awake, Start, Update across components is generally undefined within Unity.

You can test out this theory by either setting the input handler reference in the inspector for the player (and not then assigning the instance to it) OR by moving your player controller awake logic to start so we can be sure that the input handler instance has definitely been set by the time we use it.

If this is the problem then the cleaner solution is to adjust the script execution order in project settings but this method makes me uneasy and I wouldn't advise messing with this before you know this is the solution to avoid introducing more issues! :)

Comment
Add comment · Show 2 · 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 XQuirky_TurtleX · Jan 21, 2021 at 03:45 PM 0
Share

I see, later today I will try this. Thanks for the help!

avatar image XQuirky_TurtleX · Jan 21, 2021 at 07:04 PM 0
Share

Thank you, u solved it. After moving the code to Start(), it started working :D

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

122 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

Related Questions

Distribute terrain in zones 3 Answers

Prefab instance overrides and builds 1 Answer

"Object reference not set to an instance of an object" Error 1 Answer

Trying to assign new name, Unity says it can't find the object? 1 Answer

Yet another Object reference not set... 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