Changing music later in the same level & question about animation [Solved]

Hi all,

I have really been enjoying working on this game so far and have a couple of questions.

Firstly, I want the music loaded in the MusicPlayer script (in the Start scene) to change in-game, triggered by a change in a boolean value located in the FormationController script (in the Game scene). The idea being that when the Boss battle begins in the game, the music changes from the main looping track to an intro to a looping battle track.

Second question has to do with animating the player ship when it is destroyed. I have the object getting destroyed with a 2 second delay and I would like to have an animation play on top of it when its HP reach 0. What is the best way of achieving this?

If you could assist with any of this it would greatly help my understanding of how this all works.

Many thanks!

Pavlos

You can create multiple public variables in the MusicPlayer and assign multiple music tracks. In addition to this you need to create an AudioSource which can actually play the AudioClips.

Edit: I noticed that your boolean value is within the FormationController script.
You can make a reference to the MusicPlayer like you probably did with the LevelManager.
I hope this can help you.

//MusicPlayer
public AudioClip backgroundMusic; //assign in inspector
public AudioClip bossMusic; //assign in inspector
private AudioSource source;

void Start(){
source = gameObject.GetComponent< AudioSource >();
source.clip = backgroundMusic;
source.Play();
}

public void bossMusic(){
source.Stop();
source.clip = bossMusic;
source.Play();
}

//FormationController
Start(){
musicPlayer = GameObject.Find("MusicPlayer).GetComponent< MusicPlayer >();
}

if (bossBattle = true){
musicPlayer.bossMusic();
}

EDIT I just saw your edit, I’ll give that a go.**

Hey Mosjmosh, thanks for your reply.

I do have the MusicPlayer script set up like that, attached to the MusicPlayer object in the Start scene:

using UnityEngine;
using System.Collections;

public class MusicPlayer : MonoBehaviour {
static MusicPlayer instance = null;

public AudioClip startClip, gameClip, loseClip, winClip, bossIntro, bossFight;

private AudioSource music;

void Awake (){
Debug.Log("Music player Awake " + GetInstanceID());
if (instance != null && instance != this) {//Once we claim this as an instance we do not want to destroy it obv
Destroy (gameObject);
print (“Duplicate Music Player Destroyed!”);
}
else {
instance = this;
GameObject.DontDestroyOnLoad(gameObject);
music = GetComponent();
music.clip = startClip;
music.loop = false;
music.Play();
}
}

void OnLevelWasLoaded(int level){
Debug.Log("Music Player: loaded level " + level);
//music.Stop();

  if (level == 0){
  	music.clip = startClip;
  	music.Play();
  	music.loop = false;
  }
  if (level == 1) { 
  	music.clip = gameClip;
  	music.Play();
  	music.loop = true;
  }
  
  if (level == 2){
  	music.clip = loseClip;
  	music.Play();
  	music.loop = false;
  }
  if (level == 3){
  	music.clip = winClip;
  	music.Play();
  	music.loop = false;
  }

}
}

My problem lies in that the bool I am tryin to access is in the FormationController script attached to the EnemyFormation object, which is in a different scene.
I tried accessing it by doing sth like this:

	 if (!GameObject.Find("EnemyFormation").GetComponent<FormationController>().isBossTime){ 
		print ("Bool Works");
		music.Stop();
		music.clip = bossIntro;
		music.Play();
		music.loop = false;
		music.clip = bossFight;
		music.Play();
		music.loop = true;
	}
}

But unfortunately it doesn’t work :confused:

Hey, so had my sounds for the Boss Battle marked as “3D”, which is why they weren’t playing -_- I was being thick haha.

Anyway, your way works so thanks for the help :slight_smile:

Cheers!

2 Likes

Thank you for your great sharing about music, some of my suggestions about music are today’s mobile klingelton.

Privacy & Terms