- Home /
AudioSource
Hi, I have two separate scripts containing two different audios for a same gameobject. How can I make two audiosources for that gameobject?? I don't want to combine both scripts and still want to run both audios when triggered in their respective scripts. EDIT - I am using First Person Controller standard asset and it has a pre-script and sound effect. Now I have this below mentioned script which has another sound effect. So how can I play both when required??
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerHittingSound : MonoBehaviour {
AudioSource playerShotAudio; //audio plays when a player is shot
//public GameObject playerSoundObject; //reference used for accesing this script
// Use this for initialization
void Start () {
playerShotAudio = GetComponent<AudioSource>(); //it assigns the variable with the audio
}
public void PlayerSound()
{
playerShotAudio.Play(); //it plays the audio when player is shot
}
}
Answer by tormentoarmagedoom · Jul 29, 2018 at 09:18 PM
Good day.
I think best way is to have 2 Audiosource in 2 different gameobjects, so create 2 empty gameobjects childs of your object.
And add a AudioSource in this childs.
then just make each script to refear to each auidiosource.
Bye!
Damn God!! Again short and precise. You are a pro in game dev. Thanks!