- Home /
DontDestroyOnLoad alternative?
Hi, I'm in the process of developing a game. In this game if the player dies, Time.timescale
is set to 0 and a text appears saying "Click to restart game. Upon the mouse click, I have used the Appplication.LoadLevel (LevelName)
command to reload the level. Now, my game features a portal. Upon entry, the Appplication.LoadLevel (LevelName)
command is again used to load an alternate level. To avoid the portal erasing the main character, I entered the Application.DontDestroyOnLoad (gameObject)
command in the Awake ()
function to keep the player in existence. This conflicts with the restart of the game when the player dies because it creates multiple copies. Is there a way to keep the player when it enters the portal, but destroy it on the restart of the level? (I want to avoid creating another player in the additional level). Any ideas? Maybe an alternative to DontDestroyOnLoad
? Thanks in advance.
Could it involve enabling/disabling a script with the DontDestroyOnLoad
command?
Is the player object already in your level when it is loaded? Or is it spawned after the level is loaded?
Answer by raphu604 · May 18, 2014 at 12:08 AM
You could create an empty startup scene (that loads up for like 0.1 seconds when the game starts and then loads the other scenes, like the main menu for instance) with your player in it and the DontDestroyOnLoad command (and also all the other things you want to keep between loads but not instantiate multiple copies upon load).
This way you'll never have to place your player entity gameobject in any of your scenes other than that startup scene. The player never gets destroyed by anyone, just teleport him to the new location.
I have a spawning sequence that I want to occur on the restart. DontDestroyOnLoad excecutes it. Will that work with the startup scene?
Yes, that should work. Just put all the objects you want to keep persistent (as said $$anonymous$$rFijiWiji below) in that startup scene. I myself did this in my own project and it worked fine (I put all the gameplay essential stuff in there like the player, weapon and ammo management, sound effects management gameobjects and the savegame serializer that travels with you at all times).
Answer by MrFijiWiji · May 18, 2014 at 12:21 AM
You could do as raphu604 has said and create an initialisation scene where all persistent objects are created and set to DontDestroyOnLoad or as an alternative, you could create a singleton object to check if your player already exists before creating another like so:
using UnityEngine;
using System.Collections;
public class Singleton : MonoBehaviour {
//The prefab for your player to be set in the inspector.
public GameObject playerPrefab;
// Use this for initialization
void Start () {
//if we don't find a player object in the scene already...
if (!GameObject.FindGameObjectWithTag("YOUR_PLAYER_TAG"))
{
//Then instantiate one at a spawn position that you set...
Instantiate(playerPrefab, YOUR_LEVEL_SPAWN_POSITION, Quaternion.identity);
}
//Otherwise, destroy this gameobject.
else Destroy(gameObject);
}
}
Simply attach that script to an empty GameObject in your scene, set your playerPrefab, player tag and spawn position and you will only get 1 player object in each scene.
Your answer
![](https://koobas.hobune.stream/wayback/20220613145222im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Next LEVEL Load Problem. 2 Answers
Spawn carachters on locations and destroy the manager after level load 1 Answer
undoing DontDestroyOnLoad without immediately destroying the object 4 Answers
Method execution order after LoadLevel()? 1 Answer
How to begin animated skybox at same point in sequence as when the scene ended? 0 Answers