audio plays sometimes, then not others.
hi! My team is developing our first game for a game jam, that being a dungeon platformer, and we ran into a problem with our death sound. whenever we hit the spikes in the game, it plays. but when another sound plays like the coin sound, (which is just a bleep) it just doesn't play unless we wait.
any fix to this?
here's my player controller code if needed(sorry if it's messy, I'm both new to this, and in a hurry for the jam.)
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using TMPro;
public class PlayerController2D : MonoBehaviour
{
//Public Variables
public float speed;
public float jump;
public float coinCount;
public string currentScene;
public string nextScene;
public GameObject openDoor;
public GameObject closedDoor;
public GameObject doorTrigger;
public TextMeshProUGUI countText;
public AudioSource audioSource;
public AudioClip JumpSound;
public AudioClip collectSound;
public AudioClip deathSound;
public AudioClip doorOpenSound;
//Private Variables
private float moveVelocity;
private bool grounded = true;
void Start()
{
openDoor.gameObject.SetActive(false);
doorTrigger.gameObject.SetActive(false);
SetCountText();
}
void SetCountText()
{
countText.text = "Coins left: " + coinCount.ToString();
}
void Update ()
{
//Jumping
if (Input.GetKeyDown (KeyCode.Space) || Input.GetKeyDown (KeyCode.UpArrow) || Input.GetKeyDown (KeyCode.Z) || Input.GetKeyDown (KeyCode.W))
{
if(grounded)
{
GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, jump);
audioSource.PlayOneShot(JumpSound);
}
}
moveVelocity = 0;
//Left Right Movement
if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A))
{
moveVelocity = -speed;
}
if (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D))
{
moveVelocity = speed;
}
GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveVelocity, GetComponent<Rigidbody2D> ().velocity.y);
}
//Check if Grounded
void OnCollisionEnter2D()
{
grounded = true;
}
void OnCollisionExit2D()
{
grounded = false;
}
//sets trigger functions in scene
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.CompareTag("Pickup"))
{
other.gameObject.SetActive(false);
coinCount -= 1;
audioSource.PlayOneShot(collectSound);
SetCountText();
if(coinCount == 0)
{
openDoor.gameObject.SetActive(true);
closedDoor.gameObject.SetActive(false);
doorTrigger.gameObject.SetActive(true);
audioSource.PlayOneShot(doorOpenSound);
}
}
if(other.gameObject.CompareTag("Death"))
{
audioSource.PlayOneShot(deathSound);
gameObject.SetActive(false);
SceneManager.LoadScene(currentScene);
}
if(other.gameObject.CompareTag("DoorTrigger"))
{
SceneManager.LoadScene(nextScene);
}
}
}
Fixed it. Thanks to everyone who followed this question. for those who have this problem in the future, all I did was make a separate audio source called deathSource, set it as a public variable, then made the death sound go through that, so it didn't stop whenever another one was playing.
Your answer
Follow this Question
Related Questions
On iOS Build, Audio Gets Muted When App Loses Focus 0 Answers
player jump audio wont save when i put in custom audio i recorded 1 Answer
Need help: new sound isnt played correctly anymore 2 Answers
Don't reload the AudioSource when back to scene 1 Answer
Why does AudioSource.time return Infinity in Unity 5.3? 2 Answers