DontDestroyOnLoad isnt working

I have created a script which creates an object and brings it to another scene. Up to this point everything works, but if I use my Restart () function (if I’ve lost the game) this object is simply gone. The object has a DontDestroyOnLoad function and the Restart () function has that.

The object is created during the transition into the game scene, in the Play () function

public void Play()
    {
        Instantiate(Skins.itemList[MySkins.selectedSkin].Object, transform.position, transform.rotation);
        SceneManager.LoadScene("Game");
    }

When the new scene is loaded, the object is made as a child of the Player Object and its position is reset in the Start () method

void Start()
    {
        transform.position = Vector3.zero;
    }
    // Update is called once per frame
    void Update()
    {
        if (PlayerHolder == null)
        {
            PlayerHolder = transform.Find("/Player");
        }
        if (PlayerHolder != null)
        {
            transform.SetParent(PlayerHolder);
        }
    }

So far everything works, but if I die in the game and use the restart function, the object is simply gone.

 void Awake()
    {
        _Player = GameObject.FindGameObjectWithTag("Playerr");
    }
    public void Restart()
    {

        DontDestroyOnLoad(_Player);

       SceneManager.LoadScene("Game");

    }

The object also has a DontDestroyOnLoad script

    public void Awake()
    {
        DontDestroyOnLoad(this.gameObject);
    }

I appreciate any help and hope that someone can help me with the matter and thank everyone who tries to help.

Hey @armandas2005

I would recommend using the Singleton pattern.

You would use a baseclass that guarantees there is only one Instance of an Object at a time.


The code for the baseclass is:

using UnityEngine;
 
/// <summary>
/// Inherit from this base class to create a singleton.
/// e.g. public class MyClassName : Singleton<MyClassName> {}
/// </summary>
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
    // Check to see if we're about to be destroyed.
    private static bool m_ShuttingDown = false;
    private static object m_Lock = new object();
    private static T m_Instance;
 
    /// <summary>
    /// Access singleton instance through this propriety.
    /// </summary>
    public static T Instance
    {
        get
        {
            if (m_ShuttingDown)
            {
                Debug.LogWarning("[Singleton] Instance '" + typeof(T) +
                    "' already destroyed. Returning null.");
                return null;
            }
 
            lock (m_Lock)
            {
                if (m_Instance == null)
                {
                    // Search for existing instance.
                    m_Instance = (T)FindObjectOfType(typeof(T));
 
                    // Create new instance if one doesn't already exist.
                    if (m_Instance == null)
                    {
                        // Need to create a new GameObject to attach the singleton to.
                        var singletonObject = new GameObject();
                        m_Instance = singletonObject.AddComponent<T>();
                        singletonObject.name = typeof(T).ToString() + " (Singleton)";
 
                        // Make instance persistent.
                        DontDestroyOnLoad(singletonObject);
                    }
                }
 
                return m_Instance;
            }
        }
    }
 
 
    private void OnApplicationQuit()
    {
        m_ShuttingDown = true;
    }
 
 
    private void OnDestroy()
    {
        m_ShuttingDown = true;
    }
}

(Source)

To use it, you inherit the class from Singleton Instead of MonoBehaviour:

[DisallowMultipleComponent]
public class Example : Singleton<Example>
{
    //Normal MonoBehavour Code Here
   public int someInteger = -1;
}

It automatically is a MonoBehaviour and exactly one Instance is accessible at alltimes - not more and not less.

To access this Instance you refer to the static .Instance variable:

void AccessExampleSingleton()
{
    Debug.Log(Example.Instance.someInteger.ToString());
}

That’s all you have to do to have a perfectly smooth running system. I would personally recommend the Singleton-Pattern for all kinds of game managers, resource managers, pooling managers, player objects and so on. Everything that is persistent throughout the game should use this base class to avoid complicated bugs when introducing the object to the scene and removing it again, transporting it between scenes and avoiding multiple Objects. On top, it is good practice and clean code.