• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Anonymoose54 · May 17, 2014 at 02:05 AM · loadleveldontdestroyonloadselective

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Anonymoose54 · May 17, 2014 at 02:08 AM 0
Share

Could it involve enabling/disabling a script with the DontDestroyOnLoad command?

avatar image MrFijiWiji · May 18, 2014 at 12:05 AM 0
Share

Is the player object already in your level when it is loaded? Or is it spawned after the level is loaded?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Anonymoose54 · May 18, 2014 at 12:17 AM 0
Share

I have a spawning sequence that I want to occur on the restart. DontDestroyOnLoad excecutes it. Will that work with the startup scene?

avatar image raphu604 · May 18, 2014 at 12:36 AM 0
Share

Yes, that should work. Just put all the objects you want to keep persistent (as said MrFijiWiji 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).

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges