I want to make an animation for the game. The following error occurred: NullReferenceException: Upravlenie.set_State (CharState value) (at Assets / Upravlenie.cs: 20). Here is the code:

using System.Collections; using System.Collections.Generic; using UnityEngine; public class Upravlenie : MonoBehaviour { public float horizontalSpeed; float speedX; public float verticalImpulse; Rigidbody2D rb; bool isGrounded; bool Running = false; private Animator animator; private CharState State { get { return (CharState)animator.GetInteger("State"); } set { animator.SetInteger("State", (int)value); } } void Start () { rb = GetComponent<Rigidbody2D>(); } private void Flip() { isFacingRight = !isFacingRight; Vector3 theScale = transform.localScale; theScale.x *= -1; transform.localScale = theScale; } public void LeftButtonDown() { speedX = -horizontalSpeed; if (isGrounded) State = CharState.Run; } public void RightButtonDown() { speedX = horizontalSpeed; if (isGrounded) State = CharState.Run; } public void Stop() { speedX = 0; } public void OnClickJump() { if (isGrounded) rb.AddForce(new Vector2(0, verticalImpulse), ForceMode2D.Impulse); if (!isGrounded) State = CharState.Jump; } void FixedUpdate() { transform.Translate(speedX, 0, 0); if (isGrounded) State = CharState.Idle; } private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.tag == "Ground") isGrounded = true; } private void OnCollisionExit2D(Collision2D collision) { if (collision.gameObject.tag == "Ground") isGrounded = false; } } public enum CharState { Idle, Run, Jump } 

Screenshot from animator: Animator

Soma animations EXACTLY done right

I need help interacting animation with code. Please help. PS Control made via Button buttons and added event triger component

    1 answer 1

    You just lack the State variable to which the animation will react. See the example below.

    enter image description here

    Here you can see 4 variables (parameters): two float, bool and trigger.

    Everything that is transferred from the script is set in the Parameters tab (well, and vice versa, it can be delivered).

    But the Conditions (which is on the right) is already responding to the very internal variable mekanima.


    Also you forgot to do when starting animator = GetComponent<Animator>(); to initialize the variable. And the problem is most likely even in this)

    • Perezal screen. Are you talking about this? - max5937
    • @ max5937 you forgot by the way to do when starting animator = GetComponent<Animator>(); - Alexey Shimansky
    • hmmm ... the animation has appeared, but it appears when you press the button and disappears after a second and the character moves again without lifting his legs. - max5937
    • @ max5937 maybe this is because you FixedUpdate if (isGrounded) State = CharState.Idle; and perhaps when a character is on the ground, this action is worked out many times per second, as a result, it constantly seeks to Idle ; - Alexey Shimansky
    • changed to Update - the animation disappeared altogether (except for idle, it is always spinning), returned again FixedUpdate, an error appeared: Socket: failed to shutdown stream, error: The remote host forcibly terminated the existing connection. (0). - max5937