Question by
Theos82 · Jan 14, 2017 at 10:10 PM ·
sound effects
how to let play 2 same sound but places in differents prefabs ?
It's kind of hard to explain but I have created a zombie prefab which play randomly monsters sounds but when I place 2 or 3 of them I can only hear one of them...
Here is my c# Script :
using UnityEngine;
using System.Collections;
public class SoundPlayer : MonoBehaviour {
public AudioClip[] clips;
private AudioSource audioSource;
public int isActive = 0;
void Start () {
audioSource = FindObjectOfType<AudioSource>();
audioSource.loop = false;
}
private AudioClip GetRandomClip()
{
return clips [Random.Range (0, clips.Length)];
}
// Update is called once per frame
void Update () {
if(isActive == 1){
if(!audioSource.isPlaying) {
audioSource.clip = GetRandomClip();
audioSource.Play();
}
}
}
void OnTriggerEnter(Collider hit) {
if(hit.gameObject.tag =="Player") {
isActive = 1;
}
}
void OnTriggerExit(Collider hit) {
if(hit.gameObject.tag =="Player"){
isActive = 0;
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612104827im_/https://answers.unity.com/themes/thub/images/avi.jpg)