Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to destroy a game object marked "DontDestroyOnLoad" when a new scene loads?

So I created a music player to play music through all my menu and story scenes without interrupting, but then in my game scene I want to delete that music. How can destroy the playing music when my game scene loads?

Here is my music script:

#pragma strict

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

var musicPrefab : Transform;

function Start () {
    if (!GameObject.FindGameObjectWithTag("MM")) {
        var mManager = Instantiate (musicPrefab, transform.position, Quaternion.identity);
        mManager.name = musicPrefab.name;
        DontDestroyOnLoad (mManager);
    }
}
like image 506
Anton nelson Avatar asked Oct 17 '25 04:10

Anton nelson


1 Answers

Just call destroy on it directly:

Destroy(mManager);

DontDestroyOnLoad only protects the object from being destroyed when loading a new scene.

like image 144
Almo Avatar answered Oct 21 '25 04:10

Almo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!