DontDestroyOnLoad not working properly for me

Hello.

So basically i got this gamemanager and a respawn script. I also got dontdestroyonload on both the gamemanager and my player. The gameManager object handles GUI, a skilltree, respawner script and lives. On the player i have an Explode script that explodes the player and sets a playerHasDied bool to true when the player dies, and sets the bool back to false when the player respawns.

The problem is that, this works in the first level where i manually have put the GameManager in. But as soon as i play the game and the scenes loads automatically by going through doors, and DontDestroyOnLoad gets called, and i die in the next scene, the boolean doesnt get set to false when the player dies. It’s like it doesnt run my function that spawns the player back in, or well it doesnt cause i tested it.

And another problem is, its in the same category, i have a spawn point that the player is supposed to be spawning at when next scene loads as he enters the door. And i use GameObject.FIndGameObjectWithTag(“RespawnPoint”).transform to find it, but he doesnt get put at the position, he just spawns at his last position.

TLDR; DontDestroyOnLoad is just spawning my player at the position he last was at, and it doesnt run the spawn script, when the player dies.

PS sorry for the long post, but its really frustrating and i hope someone can help me if i was clear enough.

-----------//----------

Explode script

----------//----------

public class Explode : MonoBehaviour {

	//public PlayerMovement pmov;
	public BodyPart bodyPart;
	public int totalParts = 5;
	public bool playerHasDied = false;

	public void OnExplode()
	{
		GetComponent<PlayerMovement>().enabled = false;
		GetComponent<SpriteRenderer>().enabled = false;
		GetComponent<BoxCollider2D>().enabled = false;
		GetComponent<CircleCollider2D>().enabled = false;
		playerHasDied = true;
		
		var t = transform;
		
		for(int i = 0; i < totalParts; i++)
		{
			t.TransformPoint(0, -100, 0);
			BodyPart clone = Instantiate(bodyPart, t.position, Quaternion.identity) as BodyPart;
			clone.rigidbody2D.AddForce(Vector3.right * Random.Range(-50, 50));
			clone.rigidbody2D.AddForce(Vector3.up * Random.Range(100, 400));
		}
	}
}

-----------//----------

Respawner script

----------//----------

public class Respawner : MonoBehaviour {
	
	private Explode explode;
	public string level;
	private Transform respawnThingy;
	private PlayerMovement _player;
	

	void Start()
	{
		_player = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMovement>();
		explode = GameObject.FindGameObjectWithTag("Player").GetComponent<Explode>();
		respawnThingy = GameObject.FindGameObjectWithTag("RespawnPoint").transform;
	}

	public void Update()
	{
		if(explode.playerHasDied == true)
		{
			StartCoroutine("Spawn");
		}
	}

	private IEnumerator Spawn()
	{

		yield return new WaitForSeconds(0.5f);
		_player.transform.position = respawnThingy.transform.position;
		GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMovement>().enabled = true;
		GameObject.FindGameObjectWithTag("Player").GetComponent<SpriteRenderer>().enabled = true;
		GameObject.FindGameObjectWithTag("Player").GetComponent<BoxCollider2D>().enabled = true;
		GameObject.FindGameObjectWithTag("Player").GetComponent<CircleCollider2D>().enabled = true;
		_player.amountOfLives = 3;
		explode.playerHasDied = false;
		if(explode.playerHasDied == false)
		{
			Debug.Log("We're respawning"); //this doesnt execute when going to next scene ingame
		}


	}
}

and in my player script i have this

void Start()
	{
		this.transform.position = GameObject.FindGameObjectWithTag("RespawnPoint").transform.position;

		if(this.transform.position == GameObject.FindGameObjectWithTag("RespawnPoint").transform.position )
		{
			Debug.Log("We're spawning");
		}		
	}

I found the answer after hours of searching and with help from Bonfire Boy and mateiasu leading me in the right direction, thx guys :slight_smile:

The solution was to use OnLoadedLevel() and put the logic in there. As explained in this link http://answers.unity3d.com/questions/57661/method-execution-order-after-loadlevel.html