I am making a game with multiple audio tracks that a player can turn on and off via hitting or missing certain colliders.
There are 4 layers of audio tracks that I have working well but there is a script that needs to be applied to all layers yet only affect the sounds of the particular track it resides on.
What I have is a parent object called Layer1, Layer2, etc. that have an Audio Source on it.
These have a child object of a collider that will set the audio playing if hit. At the moment every time I hit an object with this script, it starts the track again. I want it that if another object has been hit and set the track playing, then if this one gets hit it will ignore it.
This script is as follows:
public class PlayNote : MonoBehaviour {
public GameObject sound;
void Start ()
{
sound = GameObject.Find(this.transform.parent.name.ToString());
sound.GetComponent("AudioSource");
}
void OnTriggerEnter(Collider other)
{
if(!AlreadyPlaying.isPlaying)
{
AlreadyPlaying.isPlaying = true;
sound.audio.Play();
this.gameObject.SetActive(false);
}
if(AlreadyPlaying.isPlaying)
{
this.gameObject.SetActive(false);
}
}
}
I have a child of this that also has a collider that when hit will stop the audio playing:
public class StopPlaying : MonoBehaviour {
public GameObject sound;
void Start()
{
sound = GameObject.Find(this.transform.parent.transform.parent.name.ToString());
sound.GetComponent("AudioSource");
}
void OnTriggerEnter(Collider other)
{
AlreadyPlaying.isPlaying = false;
sound.audio.Stop();
}
}
The last script is the one that I can’t get to work with it all and this is the one that tells the other scripts weather or not the track is already playing and sits on the Layer1/Layer2 game objects that contain the sound:
public class AlreadyPlaying : MonoBehaviour {
public bool isPlaying;
void Start ()
{
isPlaying = false;
}
}
When it runs, the AlreadyPlaying script affects all instances of the bool, not the one specific to the gameobject that the script is sitting on. I have tried using GetComponent(“AlreadyPlaying”) but I cant seem to access the boolean on each specific layer.
Any help much appreciated.