- Home /
Other
What could make the audio loop?
Im playing a sound for health pickups and from the moment they appear the pick up sound starts, the audio source doesn't have "loop" option ticked so what else could it be?
Please, edit your question, and provide the code you currently have, 99% change you have a problem with your scripts....
Answer by DragonFang17 · Oct 18, 2018 at 05:21 PM
Is it supposed to play before or after the player picks it up?
its supposed to play as the player picks it up.
Think Sonic picking up the rings as an example.
But in my case the rings make the noise from the moment they appear in a loop
You should really provide your code, how are we supposed to help without it?
I didn't really need help with code, Ive got that. I'm just asking for suggestions as to why the sound would go on a loop. $$anonymous$$y guess is its a setting issue but i dont know what
but if you need it ...
public class HealthupNoise : $$anonymous$$onoBehaviour {
public AudioSource HealthAudio; //addition
// Use this for initialization
void Start ()
{
HealthAudio = GetComponent<AudioSource> (); // addition
}
// Update is called once per frame
void OnCollisionEnter (Collision col){
if (col.gameObject.tag == "HealthPickup") {
HealthAudio.Play (); // addition
Debug.Log ("Bing!"); // addition
//Destroy (col.gameObject);
//NB: Audio is looping and "loop" option is not ticked
}
}
}
Answer by Lost_Syndicate · Oct 23, 2018 at 03:17 PM
So, your code is called every frame, So its going to loop and sound hideous. There are 2 ways of solving this.
public AudioSource audioSource;
private bool ticked;
private void Start()
{
audioSource = GetComponent<AudioSource>();
// Play it in here
audioSource.Play();
}
private void Update()
{
// OR, make a bool (as you can see "tick")
if (!ticked)
{
audioSource.Play();
ticked = true;
}
}
As you can see, those are the 2 ways i would prevent it from looping. first one i highly recommend if you want to play it on start, or even just tick play on awake, on the audio source and that should fix it. Other method is just using a bool to act as a toggle. Hope this helped you.
I was ai$$anonymous$$g for on trigger enter function, but ill try your suggestion
thank you
based on these two replies, it sounds like it's a health pickup. so use the OnCollision/OnTrigger in conjunction with AudioSource.PlayClipAtPoint. The playclipatpoint will create a one-shot audio source which will prevent the audio from cutting out on the pickup being destroyed, and will destroy itself when the audioclip finishes.
well right now ive just got the code down as
public class HealthupNoise : $$anonymous$$onoBehaviour {
void OnCollisionEnter (Collision col)
{
if (col.gameObject.tag == "HealthPickup")
{
Debug.Log ("Bing!"); // addition
}
}
}
And Bing isn't appearing on the console. The pick is tagged as HealthPickup, the box collider is set to trigger and the script is attached to the pickup so Ive no clue as to why it's not working.
Any suggestions?