Unity错误Null参考异常-C#Unity脚本

时间:2019-12-31 06:26:25

标签: c# unity3d

我正在制作一个普通的游戏,我添加了动画并得到了这个 我很新,如果这很简单

NullReferenceExpection :Object refernce not set to an instance of an object
playermovement.FixedUpdate ()

这是代码

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

public class playermovement : MonoBehaviour
{

public CharacterController2D controller;
public Animator animator;
public float runSpeed = 40f;

float horizontalMove = 0f;
bool jump = false;
bool crouch = false;

// Update is called once per frame
void Update()
{

    horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;

    animator.SetFloat("Speed",Mathf.Abs (horizontalMove));

    if (Input.GetButtonDown("Jump"))
    {
        jump = true;
    }

    if (Input.GetButtonDown("Crouch"))
    {
        crouch = true;
    }
    else if (Input.GetButtonUp("Crouch"))
    {
        crouch = false;
    }

}

void FixedUpdate()
{
    // Move our character
    controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
    jump = false;
}

} 我不知道为什么我只是一个尝试统一编码的新手 请告诉我答案

1 个答案:

答案 0 :(得分:-2)

从您的代码看来,您尚未初始化控制器