- Home /
AudioClip stuttering while loop is OFF and not using function Update?
Hi.
I have a problem. I'm making a game where there are pixel art 'BOMBS' as enemies. My player dies when he hit the bomb, and exploding animation plays, and an AudioClip should play once. However, the AudioClips starts repeating itself an infinite amount of times, resulting in a sound that sounds like my computer is stuck.
Here is the code attached to the bomb.
using UnityEngine;
using System.Collections;
public class bombScript : MonoBehaviour {
Animator animator;
SpriteRenderer bomb;
static public bool hitbomb = false;
GameObject dragon;
AudioSource bombAudio;
public AudioClip bombClip;
// Use this for initialization
void Start () {
animator = gameObject.GetComponentInChildren<Animator> ();
dragon = GameObject.FindGameObjectWithTag ("Player");
bombAudio = GameObject.Find ("SoundManager/bombAudio").GetComponent<AudioSource> ();
}
void OnTriggerEnter2D(Collider2D player) {
if(dragon.GetComponent<beta_DragonMovement>().Godmode == true)
return;
if (player.tag == "Player") {
bombAudio.PlayOneShot (bombClip);
bomb = gameObject.GetComponent<SpriteRenderer> ();
bomb.enabled = false;
hitbomb = true;
animator.SetTrigger ("death");
}
}
}
I also tried the following script:
It resulted in the same problem.
Wah wah.
using UnityEngine;
using System.Collections;
public class bombScript : MonoBehaviour {
Animator animator;
SpriteRenderer bomb;
static public bool hitbomb = false;
GameObject dragon;
AudioSource bombAudio;
// Use this for initialization
void Start () {
animator = gameObject.GetComponentInChildren<Animator> ();
dragon = GameObject.FindGameObjectWithTag ("Player");
bombAudio = GameObject.Find ("SoundManager/bombAudio").GetComponent<AudioSource> ();
}
void OnTriggerEnter2D(Collider2D player) {
if(dragon.GetComponent<beta_DragonMovement>().Godmode == true)
return;
if (player.tag == "Player") {
bombAudio.Play ();
// the bombClip is pre-set in the inspector.
bomb = gameObject.GetComponent<SpriteRenderer> ();
bomb.enabled = false;
hitbomb = true;
animator.SetTrigger ("death");
}
}
}
Answer by $$anonymous$$ · Jun 03, 2016 at 07:13 AM
This calls for some debugging. I would guess that your player hits multiple bombs that all start playing your clip. Add a Debug.log("some log string"); and check how often it is called.
You could use your static hitbomb boolean to only play the audio when hitbomb is false so that it only plays for one bomb.
Happy debugging!
Hey, thanks for your response.
I already tried the Debug.Log("playing bombAudio"); but it is just called once.
I have 3 three bombs, that all have the bombScript attached to them. The bombs are placed at random y-positions and pre-set x-positions.
The bombs are being moved to a new position when the player does not collide with them.
I tried to use a boolean, but it doesn't work either. However, if I find something that works, it's still very strange that this is happening, right?
Thanks for your time anyway!
using UnityEngine;
using System.Collections;
public class bombScript : $$anonymous$$onoBehaviour {
Animator animator;
SpriteRenderer bomb;
static public bool hitbomb = false;
GameObject dragon;
AudioSource bombAudio;
bool playedOnce = false;
// Use this for initialization
void Start () {
animator = gameObject.GetComponentInChildren<Animator> ();
dragon = GameObject.FindGameObjectWithTag ("Player");
bombAudio = GameObject.Find ("Sound$$anonymous$$anager/bombAudio").GetComponent<AudioSource> ();
}
void OnTriggerEnter2D(Collider2D player) {
if(dragon.GetComponent<beta_Dragon$$anonymous$$ovement>().Godmode == true)
return;
if (player.tag == "Player") {
if (playedOnce = false) {
bombAudio.Play ();
}
bomb = gameObject.GetComponent<SpriteRenderer> ();
bomb.enabled = false;
hitbomb = true;
animator.SetTrigger ("death");
}
playedOnce = true;
}
}
I also tried this. Here I am using a boolean to check if the audio has played already, so that after it played once, it will not play again. However, this doesn't work either... Really hope to find a solution because it's messing up my whole game.
Using the boolean playedOnce like you did in your code won't work. Each bomb has an instance of your code and each has it's own playedOnce boolean. If you want a boolean which is the same for each bomb you have to make a static bool PlayedOnce.
I can't help you with the exact solution but I hope I can send you in the right direction. If you have a solution or cause I'm interested!
I didn't find the cause, but I fixed it by just rebuilding everything related to the sound. (only took an hour or two). Ins$$anonymous$$d of having a different AudioSource for every GameObject (bomb, gem, coin, etc) I made one AudioSource called 'SoundEffects'.
Here is my final code if you are interested.
using UnityEngine;
using System.Collections;
public class GemScript : $$anonymous$$onoBehaviour {
public bool blueGem = false;
public bool greenGem = false;
public bool redGem = false;
public bool diamondGem = false;
AudioSource soundfx;
public AudioClip gemClip;
void Start () {
soundfx = GameObject.Find ("Sound$$anonymous$$anager/SoundEffects").GetComponent<AudioSource> ();
}
void OnTriggerEnter2D(Collider2D collider){
if (collider.tag == "Player") {
if (blueGem) {
Score.AddBlueGems ();
soundfx.pitch = 1.0f;
soundfx.PlayOneShot (gemClip);
} else if (greenGem) {
Score.AddGreenGems ();
soundfx.pitch = 1.2f;
soundfx.PlayOneShot (gemClip);
} else if (redGem) {
Score.AddRedGems ();
soundfx.pitch = 1.4f;
soundfx.PlayOneShot (gemClip);
} else if (diamondGem) {
Score.AddDiamondGems ();
soundfx.pitch = 1.7f;
soundfx.PlayOneShot (gemClip);
}
// changing back to same sound for every gem (ignore):
// soundfx.PlayOneShot (gemClip);
GetComponent<SpriteRenderer> ().enabled = false;
}
}
}
Thanks for your help and interest by the way! :-)