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); =;
    DontDestroyOnLoad (mManager);

Just call destroy on it directly:


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

Scripting API: Object.Destroy, The optional amount of time to delay before destroying the object. Description. Removes a GameObject, component or asset. The object obj is destroyed� instance_destroy(obj_Bullet); // destroy ALL instances of the object "obj_Bullet" The second optional flag permits you to "switch off" the Destroy Event for the instance being destroyed. By default the Destroy Event will always be performed, but if you set this flag to false then you can make the instance destroy itself and skip performing that

I created a script called Destroyer.js and attached it to the camera in the scene where I didn't want music. Then in that script I added this code, and it doesn't play anymore.

function Start() {


Destroy object with dontdestroyonload, Destroy object with dontdestroyonload. Hi,. I have some script that is attached to an object so that it isn't destroyed as you play through level 1,� How to Destroy Game Objects in Unity 3D As important instantiating and modifying gameObjects is in a game, it's equally important to destroy them when they are not required. Main reasons for destroying gameObjects include managing memory , getting rid of not-needed gameObjects (Like those that go beyond the camera and don't return), or even

You can destroy the object by calling Destroy(objectname) directy like mentioned above by Almo. You could place it in the same function call which is responsible for the change to the scene where you don't want the music to be playing.

How to detect if a GameObject has been destroyed?, When the GameObject is destroyed, does my variable go to null? the game object has been marked as destroyed by UnityEngine</returns>� Destroy: Removes a GameObject, component or asset. DestroyImmediate: Destroys the object obj immediately. You are strongly recommended to use Destroy instead. DontDestroyOnLoad: Do not destroy the target Object when loading a new Scene. FindObjectOfType: Returns the first active loaded object of Type type. FindObjectsOfType

I had the same issue so I tried Destroy(gameObject); as mentioned in answers above but It didn't work for me.

So I tried SceneManager.LoadScene("OtherSceneName", LoadSceneMode.Additive); and it worked for me. Read official docs here.

How To Destroy Game Object In Unity 3D C# Easy Methods, In this video we are going to see how to destroy a game object in 3 simple steps.Duration: 13:36 Posted: Jul 17, 2017 How to use the Destroy() function to remove GameObjects and Components at runtime. This tutorial is included in the Beginner Scripting project. Previous: Linear Interpolation Next: GetButton and GetKey

How to Instantiate and Destroy a GameObject in Unity, Destroying a GameObject in Unity requires, at its most basic, only two elements: A script that derives from MonoBehaviour, Unity's standard base� I'm making a platform jumper game (similar to Doodle Jump, if you've played that) and I want to destroy the platforms below 10 units of my player. I have a "Delete" script to do this, but I get an error: "The object of type 'GameObject' has been destroyed but you are still trying to access it." Here's my script:

How to check for queued destruction of MonoBehaviour? : Unity3D, DestroyOldInstance(); // Destroys child GameObject containing MyClass does not work either since Unity hasn't marked the behaviour as null yet. Is there any� How to destroy a game object marked “DontDestroyOnLoad” when a new scene loads? Ask Question Asked 4 years, 10 months ago. Active 1 year, 3 months ago.

How to destroy a game object in Unity C# with a don't destroy on , How do you destroy a game object in Unity C# with a don't destroy on load attached to it when it Mark Maratea, Client Architect - Gameplay. Game Maker 8 :Tutorial Destroying An Object Game Maker 8 :Tutorial Destroying An Object Skip navigation Gamemaker - enemies destroy main character - Duration: 2:49.

  • Thanks! Where would I put Destroy(mManager);? I tried putting it in my playercontroller.cs didn't do anything. And when I put it into another .js script it said "Unknown Identifier". Any ideas? :/
  • You need to keep a reference to mManager somewhere that a script can access it when you need to destroy it. I don't know how your project and code are structured, so there's no way for me to know where you need to put this or exactly how to set it up.