- Home /
no one helped so i guess i can only fix it on my own
sound.Play() will not work
i am very new to coding so I'm not the greatest person to do. i found a bug with my game that my deathSong.Play() would not work (near the bottom). some reason all the other things in Die() work perfectly fine including the death menu its just the song wont play even though i have carried the Audio Source into the correct place and everything, its not muted or disabled, it just doesnt play. is there anyone that can tell me whats going on in my 2d game?
public class Player_move : MonoBehaviour
{
public bool cantMove = false;
public bool cutSceneActivated = false;
public int playerHealth;
public int playerSpeed = 1;
public float moveX;
public int jumpPower = 125;
public bool isGrounded;
private int maxHealth = 5;
public DeathMenu deathMenu;
private float startTime;
public Score score;
public bool hit = false;
public AudioSource hurtSound;
public AudioSource mainTheme;
public AudioSource deathSong;
public float knockBackAmount = 50;
void Start()
{
playerHealth = maxHealth;
//startTime = Time.time;
}
void Update ()
{
if (hit == true)
{
hurtSound.Play();
hit = false;
}
else if (playerHealth <= 0)
{
Die();
}
else if (!cantMove)
{
playerMove();
if (moveX > 0.0f && moveX != 1.0f)
{
GetComponent<Animator>().SetBool("IsWalking", true);
}
else if (moveX < 0.0f && moveX != -1.0f)
{
GetComponent<Animator>().SetBool("IsWalking", true);
}
else if (moveX == 1.0f)
{
GetComponent<Animator>().SetBool("IsRunning", true);
}
else if (moveX == -1.0f)
{
GetComponent<Animator>().SetBool("IsRunning", true);
}
else
{
GetComponent<Animator>().SetBool("IsRunning", false);
GetComponent<Animator>().SetBool("IsWalking", false);
}
}
}
void jump()
{
GetComponent<Rigidbody2D>().AddForce(Vector2.up * jumpPower);
isGrounded = false;
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.name == "DeathZone")
{
Die();
}
}
public void cutScene()
{
if (cutSceneActivated)
{
cantMove = true;
}
else if(!cutSceneActivated)
{
cantMove = false;
}
}
void playerMove()
{
moveX = Input.GetAxis("Horizontal");
if (Input.GetButtonDown("Jump") && isGrounded)
{
jump();
}
if (moveX < 0.0f)
{
GetComponent<SpriteRenderer>().flipX = true;
}
else if (moveX > 0.0f)
{
GetComponent<SpriteRenderer>().flipX = false;
}
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(moveX * playerSpeed, gameObject.GetComponent<Rigidbody2D>().velocity.y);
}
// fix this
void Die()
{
mainTheme.enabled = false;
hurtSound.enabled = false;
//this next line of code needs to be fixed
deathSong.Play();
cantMove = true;
moveX = 0;
deathMenu.ToggleEndMenu();
}
}
Answer by zero_null · Dec 22, 2017 at 02:47 PM
You need to make sure that the deathSong AudioSource have a Audio clip and the auto Play isn't checked. If there is no clip then you must assign the clip before you ask it to play. Which clip is assigned to the deathSong ?
this image shows the other 2 audio sources being disabled as i made it cause its annoying having hit noises and i game music playing when you play the game. however nothing has changed with the death song. its not unticked or muted in any way so it should play thanks for the help by the way :) where as before play and after they dont change either thanks for the reply :)