简体   繁体   English

Unity C#-音频源未播放

[英]Unity C# - Audio Source not Playing

Recently I started making a prototype game in Unity. 最近,我开始在Unity中制作原型游戏。 But I have some problems with the audio. 但是我的音频有些问题。 I use audio.Play(); 我使用audio.Play(); but nothing happens. 但什么也没发生。 Here is the code: 这是代码:

The initialization: 初始化:

public AudioClip jumpland;
AudioSource audio;
void Start() { audio = gameObject.GetComponent<AudioSource> (); }

Playing the Sound: 播放声音:

void OnTriggerEnter2D(Collider2D col) { 
    if (down.GetComponent<BoxCollider2D> ().gameObject.tag == "block") {

        audio.clip = jumpland;
        audio.Play ();
    }
}

The audio clip is assigned but it wont play. 音频片段已分配,但不会播放。

Ok so I had to play it from the "down" game-object script. 好的,所以我必须从“向下”游戏对象脚本中播放它。 And I put it in a method so I made this method: 我把它放在一个方法中,所以我做了这个方法:

PlaySound(AudioClip clip) {

audio.clip = clip;
audio.Play();
}

and I just call this by using: 我只是通过使用以下方式调用它:

PlaySound(jumpland);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM