【问题标题】:Unity Value Assigned in Inspector Throws Null in Code在 Inspector 中分​​配的 Unity 值在代码中抛出 Null
【发布时间】:2019-07-08 23:27:11
【问题描述】:

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

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

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

检查器脚本值:

然而——这就是让我困惑的部分——由于某种原因,sfxCoin 在同一脚本中从OnCollisionEnter() 例程中调用时不是 null

所以看来 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;

        }
    }
}

【问题讨论】:

  • 发布您的整个脚本。
  • 您尝试插入的剪辑的文件格式是否可能无法通过 Unity 加载?您可以尝试将其添加到另一个脚本中,以查看它是否正常运行。
  • 感谢您的回复。剪辑实际上就在那里,甚至在同一脚本中触发 OnCollisionEnter() 时播放声音。它在 Awakw()、Start() 和 Update() 中显示为 null。 OnCollisionEnter() 中不为空。
  • 就像旁注一样:如果可能的话,我会一直在 Awake 中进行所有 GetComponent 调用。这样以后其他对象就可以在Start 使用它们
  • 试试object.ReferenceEquals(sfxCoin, null)

标签: c# unity3d


【解决方案1】:

抱歉,我发现了问题所在。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    相关资源
    最近更新 更多