- Home /
Trigger to play music needs to check if the same music is already playing
Im making a music based game and I want to use colliders to change the music playing in the background very frequently (loop and sample based, no long tracks). I got the basic script working, my problem now is I want to suppress the Play commands being called if the player enters the same collider more than once sequentially, I tried using IF statements to check if the clips on the player are the same as those the trigger is trying to load. Unfortunately it seems to just stop any other colliders changing the music too.
using UnityEngine;
public class AudioTrigger : MonoBehaviour
{
public AudioClip loopAudio;
public AudioClip oneShotAudio;
private GameObject player;
private AudioSource listeneroneshot;
private AudioSource listener;
void Start()
{
player = GameObject.Find("Player/Camera");
listener = player.GetComponent<AudioSource>();
listener.playOnAwake = false;
GameObject playeroneshot = GameObject.Find("Player/Camera/Oneshotplay");
listeneroneshot = playeroneshot.GetComponent<AudioSource>();
listeneroneshot.playOnAwake = false;
}
void OnTriggerEnter (Collider player)
{
if(!listeneroneshot.clip == oneShotAudio && !listener.clip == loopAudio)
{
listeneroneshot.Stop ();
listeneroneshot.clip = oneShotAudio;
listeneroneshot.loop = false;
listeneroneshot.Play ();
listener.Stop();
listener.clip = loopAudio;
listener.loop = true;
listener.Play();
}
}
}
Answer by Sajidfarooq · Aug 18, 2013 at 11:08 AM
You are looking at it from the wrong end. Don't worry about suppressing the "music". Worry about how you can check if the "same" collider has been accessed more than once sequentially.
Create a static variable that stores the "id" of the last collider that was hit. Then, on every collision/trigger, check if current_collision==previous_collision. Only if its false, play the music.
Thanks for that, you'll have to excuse my inexperience Ive only been attempting to code anything for a couple of months, but can you give me any more specifics? $$anonymous$$ore precisely is there an instance ID that unity uses if I use the same prefab for all the colliders? And would I have to store the last instance ID hit on a seperate script (Im guessing on the player) and reference that in the Audiotrigger script?
You can assign different "tags" to different objects to differentiate them. You can also use GetInstanceId.
You can store the tag/ID of the collider that is hit in the beginning of the same script.
Remember to mark this question as "answered" if your problem was solved.
I haven't done much with this part of my project yet, if I get it to work with your approach then I will mark it as answered.
Your answer
Follow this Question
Related Questions
If statement failing OnTriggerEnter 0 Answers
Distribute terrain in zones 3 Answers
Script to trigger all audio playing to stop, and play a different audio? 1 Answer
GetComponent().Play(); Question(s) 2 Answers
Cant get into the second if statment 1 Answer