簡體   English   中英

Inspector中分配的Unity值在代碼中拋出空

[英]Unity Value Assigned in Inspector Throws Null in Code

我是Unity的初學者,有一個問題,我無法在任何一個主板上找到答案。 創建一個非常基本的Unity C#腳本,我的Awake()函數中有以下幾行代碼:

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

第三個斷言“ Assert.IsNotNull(sfxCoin)拋出為null ,即使在Inspector中設置了硬幣AudioClip

檢查器腳本值:

在此輸入圖像描述

然而 - sfxCoin我困惑的部分 - 由於某種原因,當從OnCollisionEnter()例程在同一腳本中調用時, sfxCoin 不為 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;

        }
    }
}

對不起,我發現了問題所在。

還有一個游戲對象的第二個實例,它也使用了沒有設置sfxCoin的相同腳本。 它隱藏在層次結構中的一個節點下,所以我沒有看到它。

就像我說的那樣,我是初學者。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM