Problem in DontDestroyOnLoad

Hi,

I used DontDestroyOnLoad in my script. I have declared it on my awake function and i have put this script on one empty gameobject. Totally i have three scene, i attached this script on my first scene, when i cal the another scene i got this gameobject to that scene and again when i go back to my first scene from second scene that gameobject(wherei have put this script) is creating more like infinity.

I dont know why its happen for avoiding this i add another script for deleting that extra object. that script is here.

using UnityEngine;
using System.Collections;

public class DelectExtra : MonoBehaviour {
	
	private GameObject[] sobjet;
	private bool delete;
	
	int i;

	// Use this for initialization
	void Start () {
		i = 0;
		delete = false;
		
		sobjet = GameObject.FindGameObjectsWithTag("Script");
	}
	
	// Update is called once per frame
	void Update () {
		
		i = sobjet.Length;

		if(sobjet.Length > 10)
			delete = true;
		
		else if(sobjet.Length < 10)
		{
			delete = false;
		}
		
		if(delete)
			DestroyObject(sobjet[i]);
		
		print(sobjet.Length+ "   "+ delete);
	}
}

this is the script i used to delete the extra objects.

please help to do this

Thanks.

your telling it to not destroy itself every time you load a new scene. so if you loop through all of them, it will never destroy the original object. but the thing is, you also have that same object in the original scene. so every time you go back to the first scene, the object will be created again, and the original object will be preserved

If you want to avoid having multiple of the same object without destroying one you can create an extra scene before your other 3 and have it contain only your persistent objects and nothing else. Then you can cycle between your 3 normal scenes as much as you like because you’ll never revisit the first scene.

I usually put these things in the splash scene along with a logo or something.