【问题标题】:"Object reference not set to an instance of an object" I am getting this bug and I don't know why [duplicate]“对象引用未设置为对象的实例”我遇到了这个错误,我不知道为什么[重复]
【发布时间】:2021-08-22 22:16:38
【问题描述】:

我正在尝试为这个 healthkit 制作动画,但由于某种原因它没有。我通过添加 debug.log 对其进行了测试,当我按 Q 时,我收到错误“对象引用未设置为对象的实例”。我不知道它为什么会这样做。感谢您的帮助!

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

public class Anim_Trigger_for_healthpack : MonoBehaviour
{
    private Coroutine onHealKit = null;
    private Animator anim;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            if (onHealKit == null)
                onHealKit = StartCoroutine(HealthKit());
        }
    }

    IEnumerator HealthKit()
    {
        anim.SetBool("Qpress", true); //bug on this line
        yield return new WaitForSeconds(2.0f);
        anim.SetBool("Qpress", false);
        onHealKit = null;
    }
}

【问题讨论】:

    标签: c# unity3d animation


    【解决方案1】:

    anim 永远不会被实例化。使用Start() 中的anim = gameObject.GetComponent<Animator>(); 获取场景开始时的引用。

    或者,您可以将其设为public,然后将Animator 拖放到检查器中。

    【讨论】:

    • ty 我完全忘记了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多