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.

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 :smiley:

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! :slight_smile: