gpt4 book ai didi

c# - NullReferenceException : Object reference not set to an instance of an object - UNITY SOUNDS

转载 作者:行者123 更新时间:2023-12-03 00:35:09 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





What is a NullReferenceException, and how do I fix it?

(27 个回答)


5年前关闭。




我对统一的声音有疑问。这是我的代码:

[RequireComponent(typeof(AudioSource))]

公共(public)类 SoundManager : MonoBehaviour
{
公共(public)静态 SoundManager 实例;
private AudioSource source;
public Dictionary<SOUND_TYPE,AudioClip> sounds;
public enum SOUND_TYPE
{
DEATCH,
CATCHED
}

// Use this for initialization
void Start()
{
source = GetComponent<AudioSource>();

loadSounds();
}

// Update is called once per frame
void Update ()
{
}

public void playSound(SOUND_TYPE type)
{
source.clip = sounds[type];
source.Play();
}

public void loadSounds()
{
//loading sounds
sounds.Add(SOUND_TYPE.DEATCH, Resources.Load<AudioClip>("Sounds/AccelerationLow"));
}

}

我有一个错误,与 source.Add() 一致.

Error: NullReferenceException: Object reference not set to an instance of an object



我不知道发生了什么以及如何修复它。

最佳答案

您忘记了 sounds 的构造函数:

public Dictionary<SOUND_TYPE,AudioClip> sounds = new Dictionary<SOUND_TYPE,AudioClip>();

关于c# - NullReferenceException : Object reference not set to an instance of an object - UNITY SOUNDS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42332091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com