MissingReferenceException Error Occurs with Unity's New Input System when using "buttonEast / rightButton"

338 views Asked by At

I'm getting this error in Unity after pressing the relative "eastButton":

Error: MissingReferenceException while executing 'canceled' callbacks of 'Gameplay/Block[/DualShock4GamepadHID/buttonEast,/Mouse/rightButton]' UnityEngine.InputSystem.InputActionAsset:OnDestroy () (at Library/PackageCache/[email protected]/InputSystem/Actions/InputActionAsset.cs:794)

I'm attempting to set up 'block' functionality within my game which simply locks the player's movement then disables my "canBeHit" bool which will then put the player in a state where they cannot be hit, however upon interacting with the eastButton all player movement is completely locked preventing you from moving, however rotation is still possible. Another part I'd like to note is that I'm working with the Mirror package so I can network my game, unsure if this could also be causing problems. Would really appreciate if someone could point out where I've gone wrong, I'd be very grateful.

PlayerMovementTest.cs

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using Mirror;

public class PlayerMovementTest : NetworkBehaviour
{

    [Header ("Default")]
    Rigidbody rb;
    PlayerInput playerInput;
    PlayerControls playerInputActions;
    public bool isStunned;
    public float stunLockTimer;
    public float stunLockTimerEnd;
    public Animator animations;
    public ParticleSystem stunParticle;

    [Header("Movement")]
    public float speed;
    public float baseSpeed;

    [Header ("Dodge")]
    public float dodgeMultiplier;
    public float dodgeTimer;
    public float dodgeTimerMax;
    public int dodgeCheck;
    public float howLongDodgeLast;
    public bool isDodging;
    public GameObject enemyPlayer;
    public GameObject dodgeParticle;

    [Header("Attack")]
    public int attackReset;
    public float attackTimer;
    public float attackTimerResetMax;
    public float attackPlayingTimerEnd;
    public float lungeAttackLungeMultiplier;
    public bool isAttacking;
    public GameObject attackParticle;
    public GameObject attackHitboxObject;
    public GameObject lungeAttackHitboxObject;


    [Header("Block")]
    public bool canBeHit;

 
    private void Awake()
    {

        playerInput = GetComponent<PlayerInput>();
        rb = GetComponent<Rigidbody>();
        playerInputActions = new PlayerControls();

        // freezes rotation
        rb.freezeRotation = true;
        // enables the input controller
        playerInputActions.Gameplay.Enable();
        
        playerInputActions.Gameplay.Attack.performed += Attack;
        playerInputActions.Gameplay.Attack.canceled += Attack;
        playerInputActions.Gameplay.Dodge.performed += Dodge;
        playerInputActions.Gameplay.Block.canceled += Block;
        playerInputActions.Gameplay.Block.performed += Block;
       

        isStunned = false;
    }

    public void Update()
    {

        if (!isLocalPlayer)
            return;

        // dodge timers and reset
        if (dodgeCheck != 1)
            dodgeTimer += Time.deltaTime;

        if (dodgeTimer > dodgeTimerMax)
        {
            dodgeCheck += 1;
            dodgeTimer = 0;
        }

        // turns on the slide particle when sliding 
        if (isDodging == true)
            dodgeParticle.SetActive(enabled);
        else
            dodgeParticle.SetActive(false);

        // sets how long the EFFECT of the dash lasts for
        // not how long the player will move but how long 
        // they will be able to collide with another player 
        // and stun them
        if (dodgeTimer > howLongDodgeLast)
            isDodging = false;

        // attack timers and reset
        if (attackReset != 1)
        {
            attackTimer += Time.deltaTime;
        }

        // after a certian amount of time the player attack goes away
        if (attackTimer > attackPlayingTimerEnd)
        {
            //attackParticle.SetActive(false);
            attackHitboxObject.SetActive(false);
            lungeAttackHitboxObject.SetActive(false);

        }

        if (attackTimer > attackTimerResetMax)
        {
            attackReset += 1;
            attackTimer = 0;
        }

        // makes it so attacks dont go into the minuses
        if (attackReset < 0)
            attackReset = 0;

        // when player gets stunned it starts a timer where they cannot do any moves
        if (isStunned == true)
        {
            stunLockTimer += Time.deltaTime;

            if (stunLockTimer > stunLockTimerEnd)
            {
                isStunned = false;
                stunLockTimer = 0f;
                speed = baseSpeed;
            }
        }

        // chanegs between animations depending on how fast the player is moving 
        if (rb.velocity.magnitude > 4f)
            animations.SetFloat("Walk", rb.velocity.magnitude); 
      if (rb.velocity.magnitude < 4f)
            animations.SetFloat("Idle", rb.velocity.magnitude);
        

            // if player is stunned then play stun particle
            if (isStunned == true)
            stunParticle.Play();
        else
            stunParticle.Stop();


       //edited out the print as it got distracting
       // print(rb.velocity.magnitude);
    }

    // for dodge stun
    public void OnCollisionEnter(Collision col)
     {  
        // if you hit a player
       if( col.gameObject.tag == "Player")
       {
            // and in dodge, number picked just as an idea of how long the dash could last
            if (dodgeTimer > .1f && dodgeTimer < 1f)
            { 
                // sets the player hit as a gameobject that cabn be affected in script
                enemyPlayer = col.gameObject;
                // decreases speed (or in this case increases as i cannot test 2 players 
                enemyPlayer.GetComponent<PlayerMovementTest>().speed = 0;
                enemyPlayer.GetComponent<PlayerMovementTest>().isStunned = true;
            }
       }
    }

    public void FixedUpdate()
    {
        // calls movement function
        Movement();
    }

    public void Attack(InputAction.CallbackContext context)
    {
     
        if (attackReset == 1 && isStunned == false)
        {
            isAttacking = true;
            attackHitboxObject.SetActive(enabled);
            attackReset -= 1;
            animations.SetTrigger("Attack");
            attackParticle.SetActive(enabled);
        }

        //if (attackReset == 1 && isStunned == false)

        /*
        if (context.performed)
            {
                print("attack start");
            }
            if (context.canceled)
            {
            // for luneg attack
                rb.AddForce(transform.forward * lungeAttackLungeMultiplier, ForceMode.Impulse);
            isAttacking = true;
            lungeAttackHitboxObject.SetActive(enabled);
            attackReset -= 1;
           // animations.SetTrigger("Attack");
            //   attackParticle.SetActive(enabled);
                print("attack end");
            }*/
    }

    public void Dodge(InputAction.CallbackContext context)
    {
        // checks if the player has a dodge 
        if (dodgeCheck == 1 && isStunned == false)
        {
            // used for lockign slide in place and particle effect
         isDodging = true;
         // adds a force to the player, spped can be adjusted with dodgeMultiplier
         rb.AddForce(transform.forward * dodgeMultiplier, ForceMode.Impulse);
         // removes dodge
         dodgeCheck -= 1;
         animations.SetTrigger("Slide");
        }
    }

    public void Block(InputAction.CallbackContext context)
    {
       if (isStunned == false)
       {     // when you hold the button, you lose your speed
        if (context.performed)
        {
            canBeHit = false;
            speed = 3;
            animations.SetBool("Block", true);
        }
       
        // when you release it your speed goes back to normal
        if (context.canceled)
        {
            canBeHit = true;
            speed = 15;
            animations.SetBool("Block", false);
            }
        }
    }

    public void Movement()
    {
        // used for locking slide in place, no rotation 
       if (isDodging == false)
       {
            //player movement, can only use vector2 for controller so we use a vector3
            // but store the x and z in a vector 2
            Vector2 inputVector = playerInputActions.Gameplay.Walk.ReadValue<Vector2>();
            Vector3 tempVec = new Vector3(inputVector.x, 0, inputVector.y);
           
                // adds force to the vector, do this seperately so we can use
                //the variable for the player rotation
                rb.AddForce(tempVec * speed, ForceMode.Force);
            
            if (tempVec != Vector3.zero)
            {
            // finds the direction the player is moving
            Quaternion targetRotation = Quaternion.LookRotation(tempVec);
            // rotates players towards the way they are facing
            targetRotation = Quaternion.RotateTowards(transform.rotation, targetRotation, 360 * Time.fixedDeltaTime);

            rb.MoveRotation(targetRotation);
            }
        }
      
    }
}

If any additional information is needed to solve my problem I'm more than happy to submit any necessary info.

0

There are 0 answers