我收到警告“字段'Enemy.AI.animator'从未分配”

时间:2019-12-30 12:48:50

标签: c# unityscript

我制定了一个代码,使敌人在2点之间移动,当他达到最低点时,他将停止。 我收到错误消息是因为它说动画师没有分配变量,但是我在start方法中进行了设置。它还说,动画师将始终使用默认值null,我不知道该怎么办。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyAI : MonoBehaviour
{
    //assigning variables
    public float moveSpeed;
    public Transform target;
    public float chaseRadius;
    public float attackRadius;
    private Rigidbody2D rb;
    private Animator animator;

    // Start is called before the first frame update
    void Start()
    {
        //getting the necessary components
        animator.GetComponent<Animator>();
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        CheckDistance();
    }
    //checking distance between 2 points and setting the rigid body to move
    void CheckDistance() 
    {
        if(Vector3.Distance(target.position, transform.position) <= chaseRadius && Vector3.Distance(target.position, transform.position) > attackRadius)
        {


             Vector3 temp = Vector3.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime);
             ChangeAnim(temp - transform.position);
             rb.MovePosition(temp);
        }
    }
   //changing animations of the object
    private void ChangeAnim(Vector2 direction)
    {
        if (Mathf.Abs(direction.x) > Mathf.Abs(direction.y))
        {
            Debug.Log("x is greater");
            if(direction.x > 0) 
            {
                animator.SetFloat("moveX", direction.x);
            }
            else if(direction.x < 0) 
            {
                animator.SetFloat("moveX", direction.x);
            }
        }else if (Mathf.Abs(direction.x) < Mathf.Abs(direction.y))
        {
            Debug.Log("y is greater");
            if (direction.y > 0)
            {
                animator.SetFloat("moveX", direction.y);
            }
            else if (direction.y < 0)
            {
                animator.SetFloat("moveX", direction.y);
            }
        }
    }
}

Player Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    public float speed;
    private Rigidbody2D myRigidbody;
    private Vector3 change;
    private Animator animator;

    // Start is called before the first frame update
    void Start()
    {
        animator = GetComponent<Animator>();
        myRigidbody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    { 
        change = Vector3.zero;
        change.x = Input.GetAxisRaw("Horizontal");
        change.y = Input.GetAxisRaw("Vertical");
        UpdateAnimationAndMove();
    }

    void UpdateAnimationAndMove() 
    {
        if (change != Vector3.zero)
        {
            MoveCharacter();
            animator.SetFloat("moveX", change.x);
            animator.SetFloat("moveY", change.y);
            animator.SetBool("moving", true);
        }
        else
        {
            animator.SetBool("moving", false);
        }

    }

    void MoveCharacter() 
    {
        myRigidbody.MovePosition
            (
            transform.position + change * speed * Time.deltaTime
        );

    }

}

1 个答案:

答案 0 :(得分:0)

您应该在EnemyAI类中添加一个构造函数,该构造函数可以传递给动画对象。

public EnemyAI(Animator animator) 
{
  this.animator = animator;
}

如果您在PlayerMovement中查看启动功能,则会看到以下内容:

void Start()
{
    animator = GetComponent<Animator>();
    myRigidbody = GetComponent<Rigidbody2D>();
}

您的EnemyAI拥有以下功能:

void Start()
{
    //getting the necessary components
    animator.GetComponent<Animator>();
    rb = GetComponent<Rigidbody2D>();
}

您可以看到最后一个未分配动画师。 将您的EnemyAI启动功能更改为此:

void Start()
{
    //getting the necessary components
    this.animator = animator.GetComponent<Animator>();
    rb = GetComponent<Rigidbody2D>();
}