简体   繁体   English

Inspector中分配的Unity值在代码中抛出空

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

I'm a beginner at Unity with a problem for which I've not been able to find an answer on any of the boards. 我是Unity的初学者,有一个问题,我无法在任何一个主板上找到答案。 Creating a very basic Unity C# script, I have the following lines of code in my Awake() function: 创建一个非常基本的Unity C#脚本,我的Awake()函数中有以下几行代码:

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

The third assertion " Assert.IsNotNull(sfxCoin) throws as null , even though the coin AudioClip is set in the Inspector: 第三个断言“ Assert.IsNotNull(sfxCoin)抛出为null ,即使在Inspector中设置了硬币AudioClip

Inspector script values: 检查器脚本值:

在此输入图像描述

However -- and this is the part that has me baffled -- for some reason sfxCoin is not null when invoked in the same script from an OnCollisionEnter() routine 然而 - sfxCoin我困惑的部分 - 由于某种原因,当从OnCollisionEnter()例程在同一脚本中调用时, sfxCoin 不为 null

So it appears Unity does register the object with the code -- eventually -- but the assertions fail with the initial Awake() , Start() , and Update() methods. 所以看来Unity确实用代码注册了对象 - 最终 - 但是断言失败了,最初是Awake()Start()Update()方法。

And this only is happening with sfxCoin . 这只发生在sfxCoin sfxJump and sfxDeath do not have this issue. sfxJumpsfxDeath没有这个问题。

Any help would be appreciated 任何帮助,将不胜感激

Entire script is below: 整个脚本如下:

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;

        }
    }
}

sorry, I found out what the problem was. 对不起,我发现了问题所在。

There was a second instance of a game object that was also using the same script that did not have sfxCoin set. 还有一个游戏对象的第二个实例,它也使用了没有设置sfxCoin的相同脚本。 It was hidden under a node in the hierarchy so I didn't see it. 它隐藏在层次结构中的一个节点下,所以我没有看到它。

Like I said, I'm a beginner at this. 就像我说的那样,我是初学者。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Unity3D-即使在检查器中分配了变量,也未分配 - Unity3D - Variable Unassigned even though assigned in Inspector 如何从统一检查器将值传递给构造函数 - How to pass value to constructor from unity inspector Unity行为未在检查器中设置的属性,输出null但不== null - Unity behavior Property not set in inspector, prints null but doesn't == null 如何检查脚本检查Unity Inspector空参数和不正确的值 - How to check a Script for Unity Inspector null parameters and incorrect values 检查是否在 Unity3D 2019.3.05f 中的检查器中分配了 GameObject - Check if a GameObject has been assigned in the inspector in Unity3D 2019.3.05f 我可以将多个资产分配给 Unity 中的脚本,而在检查器中只允许将 1 个资产分配给它吗? - Can I assign multiple assets to a script in Unity which only allows 1 asset to be assigned to it in the inspector? 如何从代码(Unity)在检查器中打开资产的孩子 - How to open a child of asset in inspector from code (Unity) 检查器值无法从Unity3d中的另一个类访问 - Inspector value cannot access from another class in Unity3d Unity3D:是否有一种简单的方法可以在检查器中给一个最小值/最大值? - Unity3D: Is there a easy way to give a min/max to a value in the inspector? 在Unity中,如何在Inspector中更改布尔值时触发Set方法? - In Unity, How to Trigger the Set Method When Changing Boolean Value in the Inspector?
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM