Audio 统一-为什么我不能通过脚本播放我需要的声音?

Audio 统一-为什么我不能通过脚本播放我需要的声音?,audio,unity3d,Audio,Unity3d,我有作为我的播放器对象的孩子的音频源,在那里我得到了我正在使用的脚本 因此,我在主类中实现音频源: public AudioSource good_morning; public AudioSource stroke_message; public AudioSource citysound; public AudioSource silence; 然后在void Start()中,我尝试获取组件并播放其中的一个。但是什么也没有发生。。你能帮我吗 citysound = GetComponen

我有作为我的播放器对象的孩子的音频源,在那里我得到了我正在使用的脚本

因此,我在主类中实现音频源:

public AudioSource good_morning;
public AudioSource stroke_message;
public AudioSource citysound;
public AudioSource silence;
然后在void Start()中,我尝试获取组件并播放其中的一个。但是什么也没有发生。。你能帮我吗

citysound = GetComponentInChildren<AudioSource>();
good_morning = GetComponentInChildren<AudioSource> ();
stroke_message = GetComponentInChildren<AudioSource> ();
silence = GetComponentInChildren<AudioSource>();
good_morning.Play ();
citysound=getComponentChildren();
早上好=getComponentChildren();
stroke_message=getComponentChildren();
静默=getComponentChildren();
早上好,玩吧;

我怀疑的是,您的
音频源在场景中没有指定音频剪辑

我敢肯定的是,您不完全理解如何使用
getComponentChildren()。它通过变换的子级进行迭代;在其中一个音频源中检测到
音频源后,它将停止并返回该音频源。因此,基本上,所有变量都将设置为相同的
AudioSource
。我建议您从第二个代码段中删除前4行,并从Unity editor手动分配
音频源
,以便确保每个音频源都分配给正确的变量。

void start()
{
AudioSource citysound = GetComponent<AudioSource>();
AudioSource good_morning = GetComponent<AudioSource> ();
AudioSource stroke_message = GetComponent<AudioSource> ();
AudioSource silence = GetComponent<AudioSource>();
good_morning.Play ();
}
void start()
{
AudioSource citysound=GetComponent();
AudioSource good_morning=GetComponent();
AudioSource stroke_message=GetComponent();
音频源静音=GetComponent();
早上好,玩吧;
}

尤其是因为音频源被声明为公共变量。谢谢!我不需要使用getComponentChildreno我不需要删除它:AudioSource citysound=GetComponent();AudioSource good_morning=GetComponent();AudioSource stroke_message=GetComponent();音频源静音=GetComponent();