Background music error

hi,I’m new to unity and I’m following a course on it on YouTube. I’ve created an empty game object and I’ve added a script to it so i have a coin counter to it and that works fine. I’ve also made it so that a song will play in the background and it won’t restart every time you die. Well,this sort of works since every time you respawn the music continues on but it makes a copy of itself in the hierarchy and restarts so i now have two copies of the song playing at the same time but they are at different points of the song.Can someone please help me.

#pragma strict

static var currentScore : int = 0;

var offsetY : float = 40;
var sizeX : float = 100;
var sizeY : float = 40;

var musicPrefab : Transform;

function Start () {
currentScore = 0;

if (!GameObject.FindGameObjectWithTag(“MM”)) {
var mManager = Instantiate (musicPrefab, transform.position, Quaternion.identity);
mManager.name = musicPrefab.name;
DontDestroyOnLoad (mManager);
}
}

function OnGUI () {
GUI.Box (new Rect (Screen.width/2-sizeX/2, offsetY, sizeX, sizeY), "Score: " + currentScore);
}

this is the script if that helps in any way.

http://unitypatterns.com/singletons/
http://wiki.unity3d.com/index.php?title=Singleton

thanks for the help but i don’t think that’s what I’m looking for.Maybe you could just write out the code that would work as I’m a bit of a noob to unity but thank anyways!

First guess would be that your musicPrefab isn’t tagged as MM

thanks for the help but could you show how to tag it as im a noob to unity. Could you like write the script to tag my musicPrefab as MM?
thanks

thanks but could you tell me where abouts in the script it should go?Thanks!

You can also just do it via the Editor

I tried tagging it the editor but now the music won’t play at all! Could you please just copy and paste the script i wrote and fix it as i’m really confused as how to tag an object and where it should go in the script? Sorry. :frowning:

If your object is tagged correctly then the code should work as you have it - so I’m not sure how else I can help you.

Note that you should be tagging the prefab and not the object that has this script on it.

Maybe you should do some more reading in the links I provided to get a better understanding of what tags are and how they work. It seems like you don’t have a good grasp of what that code is supposed to be doing.