DontDestroyOnLoad reset at certain point

I’m using this script so the score won’t be destroyed on load but when I when I click play again the score stays were it was but I want it to reset after I’ve died so easiest thing I could think of would be to “restart” it when the game scene loads again but the problem I got is I have no idea on how to make it work… if someone knows, please give me an good answer, thx.

using UnityEngine;

using System.Collections;

public class SingularObject : MonoBehaviour

{

void Awake() 

{

    string prevName = name;

    name = "";

    GameObject other;

    if (other = GameObject.Find(prevName))

    {

        other.SendMessage("OnSceneReloaded", SendMessageOptions.DontRequireReceiver);

        DestroyImmediate(gameObject);

        return;

    }

    name = prevName;

    DontDestroyOnLoad(gameObject);

	
}

}

I know this question is quite old, however I found a very simple way to reset the DontDestroyOnLoad state of an object. I hope it can help others running into the same problem.

As an object targeted by DontDestroyOnLoad just gets moved to a separate scene called “DontDestroyOnLoad” by Unity, the process can be undone by moving the object to it’s original scene.
A simple implementation could look like this:

using UnityEngine;
using UnityEngine.SceneManagement;

public abstract class SomeObject : Monobehaviour {

    protected Scene defaultScene;

    void Awake() {
        defaultScene = gameObject.scene;
    }

    public void ResetDestroyOnLoad() {
        SceneManager.MoveGameObjectToScene(gameObject, defaultScene);
    }
}

It’s not possible to remove an object’s ‘DontDestroyOnLoad’ status. By doing so, you are telling the engine that you want to manually take control of said object’s lifecycle! To get rid of the object, you could put something inside of your OnLevelLoaded function, which checks what level just got loaded, and manually deletes your scores if it’s the ‘main menu’ level again. Or, alternatively, you could just find and destroy the scores object from the previous game when you press the play button again!