c# - Unity:无法让动画工作,得到 NullReferenceException
问题描述
我到处寻找并尝试了一切来让我的代码工作,但我仍然无法让它工作。
如果有人可以尝试帮助我解决此问题,我将不胜感激。
//PLAYER JUMP MOVEMENT
if (onGround == true && Input.GetKey("w"))
{
onGround = false;
rb.velocity = new Vector2(rb.velocity.x, playerJumpSpeed * Time.deltaTime);
isJumping = true;
}
if (playerLives <= 0)
{
Destroy(gameObject, 5f);
}
if (isJumping == true)
{
anim.SetBool("isJumping", true);
}
if (isJumping == false)
{
anim.SetBool("isJumping", false);
}
出现NullReferenceException
在:
anim.SetBool("isJumping", true);
和
anim.SetBool("isJumping", false);
解决方案
public class playerController : MonoBehaviour
{
private Animator anim;
// Start is called before the first frame update
void Start()
{
Animator anim = GetComponent<Animator>();
}
这段代码给我错误的原因是我不应该在 Start 中将 anim 声明为 Animator。为简化起见,代码应如下所示:
public class playerController : MonoBehaviour
{
private Animator anim;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
}
推荐阅读
- java - 如何在 Java 中导出 Google analitycs 数据?
- c - 为什么我的 tcp syn 消息没有得到 tcp syn ack 所以我无法连接到服务器
- php - 如何在 macthing 正则表达式数组中搜索字符串
- javascript - 如何将 OrbitControl by Three 与 React Native 和 Expo 结合使用
- python-3.x - 使用python从给定列表中每个月的最后一个交易日
- r - 使用 MacOSX 上的文档中的示例解析 R nanotime 中的错误
- mysql - 使用 after* before* 钩子对加密字段进行后续查询
- mongodb - 在独立模式下为 mongodb 运行 kafka connect 时出现 NoClassDefFoundError
- r - R数据框使用上一行中当前行中的值
- android - React Native - 通过引用聚焦一个可触摸的对象