Search Unity

Make music play when nothing is colliding.

Discussion in 'Scripting' started by Deleted User, May 24, 2015.

  1. Deleted User

    Deleted User

    Guest

    So right now I have a script which plays music when it collides with an enemy but if the enemy dies it doesn't trigger because its been destroyed rather than existing the trigger. I would really like to learn from this and I've felt like I've exhausted all the options I can think of. It would be easy if it was just one enemy but this needs to work for multiple enemies.

    This is my script.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class InBattle : MonoBehaviour
    5. {
    6.     public GameObject player;
    7.     public AudioSource audioSource;
    8.     public AudioSource mainSource;
    9.     bool outCombat = true;
    10.  
    11.     void Update()
    12.     {
    13.         if(outCombat == true)
    14.         {
    15.             audioSource.Pause();
    16.             mainSource.UnPause();
    17.             Debug.Log ("Carrying on playing main music");
    18.         }
    19.  
    20.         Vector3 pos = new Vector3 (player.transform.position.x, player.transform.position.y, (player.transform.position.z));
    21.         transform.position = pos; //find position of player and move to it every frame.
    22.     }
    23.  
    24.     void OnTriggerEnter(Collider col) //if battlechecker collides  with enemy
    25.     {
    26.         if (col.tag == "Enemy")
    27.         {
    28.             inBattleDis ();
    29.         }
    30.         else
    31.         {
    32.             outCombat = true;
    33.         }
    34.     }
    35.  
    36.     void OnTriggerExit(Collider col) //if battlecheker exits the enemy
    37.     {
    38.         if (col.tag == "Enemy")
    39.         {
    40.             audioSource.Pause();
    41.             mainSource.UnPause();
    42.         }
    43.     }
    44.     public void inBattleDis() //if it is in combat pause main music and plays battle music
    45.     {
    46.         outCombat = false;
    47.         if (outCombat == false)
    48.         {
    49.             audioSource.Play ();
    50.             mainSource.Pause ();
    51.         }
    52.     }
    53. }
     
    Last edited by a moderator: May 24, 2015
  2. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    You could add your play-Music-logic to the destroy-the-object-part of your code.
     
  3. Deleted User

    Deleted User

    Guest

    tried that but if i have 2 enemies one would still be playing battle music and the other one would be playing the scene music.
     
  4. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    How about moving you entire play-music-logic out of the event handlers and to adding a global flag somewhere? Set the flag to control which Music should be played, and test onupdate if the desired music is played.