c# - Inspector 中分​​配的 Unity 值在代码中抛出 Null

标签 c# unity3d

我是 Unity 的初学者,有一个问题我无法在任何论坛上找到答案。创建一个非常基本的 Unity C# 脚本,我的 Awake() 函数中有以下代码行:

Assert.IsNotNull(sfxJump);
Assert.IsNotNull(sfxDeath);
Assert.IsNotNull(sfxCoin);

第三个断言“ Assert.IsNotNull(sfxCoin) 抛出为 null,即使硬币 AudioClip 已在 Inspector 中设置:

检查器脚本值:

enter image description here

然而——这是让我感到困惑的部分——由于某种原因,sfxCoin 在相同的调用时 not null来自 OnCollisionEnter() 例程

的脚本

所以看起来 Unity 确实用代码注册了对象——最终——但是断言失败了,初始的 Awake()Start()Update() 方法。

这只发生在 sfxCoin 上。 sfxJumpsfxDeath 没有这个问题。

任何帮助将不胜感激

完整脚本如下:

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

public class Player : MonoBehaviour
{
    [SerializeField] private float jumpForce = 100f;
    [SerializeField] private float forwardMomentum = 5f;
    [SerializeField] private AudioClip sfxJump;
    [SerializeField] private AudioClip sfxDeath;
    [SerializeField] private AudioClip sfxCoin; 

    private Animator anim;
    private Rigidbody Rigidbody;
    private bool jump = false;
    private AudioSource audioSource;  

    private void Awake()
    {
        Assert.IsNotNull(sfxJump);
        Assert.IsNotNull(sfxDeath);
        Assert.IsNotNull(sfxCoin);
    }

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
        Rigidbody = GetComponent<Rigidbody>();
        audioSource = GetComponent<AudioSource>();        
    }

    // Update is called once per frame
    void Update()
    {
        if (!GameManager.instance.GameOver() && GameManager.instance.GameStarted())
        { 
            if (Input.GetMouseButton(0))
            {
                GameManager.instance.PlayerStartedGame();

                anim.Play("Jump");
                audioSource.PlayOneShot(sfxJump);
                Rigidbody.useGravity = true;
                jump = true;
            }
        }
    }

    private void FixedUpdate()
    {
        if (jump)
        {
            jump = false;
            Rigidbody.velocity = new Vector2(0, 0);
            Rigidbody.AddForce(new Vector2(forwardMomentum, jumpForce), ForceMode.Impulse);
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        switch (collision.gameObject.tag)
        {
            case "obstacle":
                Rigidbody.AddForce(new Vector2(-50, 20), ForceMode.Impulse);
                Rigidbody.detectCollisions = false;
                audioSource.PlayOneShot(sfxDeath);
                GameManager.instance.PlayerCollided();
                break;
            case "coin":

                audioSource.PlayOneShot(sfxCoin);
                GameManager.instance.Score(1);
                print("GOT COIN");
                break;

        }
    }
}

最佳答案

抱歉,我知道是什么问题了。

游戏对象的第二个实例也使用相同的脚本,但没有设置 sfxCoin。它隐藏在层次结构中的一个节点下,所以我没有看到它。

就像我说的,我是这方面的初学者。

关于c# - Inspector 中分​​配的 Unity 值在代码中抛出 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54698828/

相关文章:

c# - 我对一些反射代码感到困惑,正在寻找见解

c# - unity raycast,on raycast leave,怎么办? C#

c# - 从 UWP 应用程序请求永久访问文件夹

c# - Collection<T> 与 List<T> 您应该在界面上使用什么?

c# - System.Threading.Tasks 不遵守启动规则?

c# - unity Recttransform 包含点

java - 如何使用非主要 Activity 在统一游戏中捕获自定义网址?

c# - 如何以编程方式设置 TARGETDIR?

c# - 如何使用 nHibernate 存储 128 位整数?

unity3d - 下一次追加之前的Unity dotween延迟不起作用