Anim.SetFloat不起作用

时间:2018-03-16 21:28:13

标签: c# windows

我收到此错误:

  

NullReferenceException:未将对象引用设置为对象的实例

     

UnityEditor.Graphs.AnimationStateMachine.GraphGUI.SyncGraphToUnitySelection(布尔力)(在C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/AnimationStateMachine/GraphGUI.cs:310)
  UnityEditor.Graphs.AnimatorControllerTool.DetectAnimatorControllerFromSelection()(在C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/Animation/AnimatorControllerTool.cs:734)
  UnityEditor.Graphs.AnimatorControllerTool.OnEnable()(在C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/Animation/AnimatorControllerTool.cs:747)

这是我的代码(团结2017.3.0f3)

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

    public float moveSpeed;

    private Animator anim;

    // Use this for initialization
    void Start () {
        anim = GetComponent<Animator>();    
    }

    // Update is called once per frame
    void Update () {
        if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
        {
            transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));          
        }

        if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
        {
            transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
        }
    }

    anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
    anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical")); 
}

1 个答案:

答案 0 :(得分:0)

我不知道这是否有效,但你可以试试这个:

public GameObject animObject;
private Animator anim;

void Start () {
anim = animObject.GetComponent<Animator>();
}

In&#34; animObject&#34;获取您打算制作动画的GameObject的动画制作者。