Victory music when enemy dies, help
Hi!
I am having issues creating a victory music for my game. I am making a 2d platformer where the main character tries to save her grandma from an evil plant. What I want to happen is that when the plant boss dies it would stop playing my level music and switch into another soundtrack + open a door to release the granma. But my unity keeps popping up with 17 error codes and I am confused. I always end up erasing any changes I make because of that.
Here is the script that actually works:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Enemy : MonoBehaviour { public Animator animator; public int maxHealth = 100; int currentHealth; public GameObject disabler;
// public float attackRange = 0.5f;
// public int attackDamage = 40;
// public Transform attackPoint;
// public LayerMask playerLayers;
//Set health to max health
void Start()
{
currentHealth = maxHealth;
}
//Calculate damage
public void TakeDamage(int damage)
{
currentHealth -= damage;
//Play hurt animation
Debug.Log("Enemy got hit!");
animator.SetTrigger("enemy_hit");
//Kill when health goes under 0
if (currentHealth <= 0)
{
Die();
}
}
//Play death animation and disable enemy
void Die()
{
animator.SetBool("enemy_dead", true);
//Disable enemy
GetComponent<Collider2D>().enabled = false;
if (gameObject.tag == "Enemy")
{
Debug.Log("Enemy died!");
GetComponent<PingPong>().enabled = false;
}
if (gameObject.tag == "Boss")
{
Debug.Log("Boss died!");
disabler = GameObject.FindWithTag("BossDisable");
Behaviour[] behaviours = disabler.GetComponentsInChildren<Collider2D>();
foreach (var item in behaviours)
{
item.enabled = false;
}
// disabler.GetComponent().enabled = false; // GameObject[] disableall; // disableall = GameObject.FindGameObjectsWithTag("BossDisable").SetActive(false); } this.enabled = false; }
// Damage the player
// void Attack()
// {
// //Attack animation
// //animator.SetTrigger("vesimeloni_attack");
//
// Collider2D[] hitPlayer = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, playerLayers);
//
// //Damage the player
// foreach (Collider2D player in hitPlayer)
// {
// Debug.Log("Player got hit!");
// player.GetComponent<PlayerCombat>().TakeDamage(attackDamage);
// }
// }
//Draw sphere to damage collider // void OnDrawGizmosSelected() // { // if (attackPoint == null) // return; // // Gizmos.DrawWireSphere(attackPoint.position, attackRange); // } }
What do I need to add to that to make the changes I want happen? Any help is appreciated, thank you.
Answer by Pathojen · Dec 08, 2021 at 04:08 PM
Try adding an audiosource to the enemy with this scrip. After that, in the Die function, add gameObject.GetComponent<Audiosource>().Play().
Your answer
Follow this Question
Related Questions
Trying to use audio causes errors when trying to use in an 'if' statement 1 Answer
How to stop and re-start audio? 1 Answer
Playing audio from streaming assets - how do I stop error being thrown if file isn't there? 0 Answers
When enemy spawns it loses the script settings. 0 Answers
Audio not working on first run of game, but after second run it does 0 Answers