- Home /
Trigger Sound more than Once
Hello! I've been trying to play an audio piece when a GameObject is set active, and that is currently going fine as shown in the code below. However, as you can see in the code, I have TWO AudioClips, one of them AudioClips is supposed to be active when the ParlimentActive bool = true, that AudioClip is named Order. The other AudioClip is stopped when the ParlimentActive bool = true, but turned back on when it is false. That is still working fine.
The Problem is that if the bool is true, it plays, but the other unwanted AudioClip also plays. Additionally, When I turn the Bool false, and then true again: it doesn't play the wanted Order audio. All help would be appreciated, thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
public class AudioInput : MonoBehaviour
{
public bool ParlimentActive = false;
public GameObject Parliment;
public AudioClip Order;
public AudioSource audioSource;
public AudioClip GS;
public AudioSource audioSource2;
void Start()
{
audioSource.clip = Order;
audioSource2.clip = GS;
}
void Update()
{
if(Parliment.activeInHierarchy == true)
{
ParlimentActive = true;
}
else if(Parliment.activeInHierarchy == false)
{
ParlimentActive = false;
}
// Seperating Line
if(ParlimentActive == true)
{
audioSource.clip = Order;
audioSource.playOnAwake = true;
audioSource2.playOnAwake = false;
return;
}
else if(ParlimentActive == false)
{
audioSource.clip = Order;
audioSource.Stop();
audioSource2.playOnAwake = true;
return;
}
audioSource.clip = Order;
}
}
Your answer
Follow this Question
Related Questions
Scripting button functionality inside class with created object's method 1 Answer
Help making multiple buttons do a specific set of actions in any order pressed 1 Answer
Adding single Event listener to multiple ui buttons via scripting 1 Answer
Cannot change Button interactable in a UnityAction 3 Answers