• 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
Question by OlivierROGER · Apr 28, 2018 at 03:09 PM · gameobjectdestroydontdestroyonloadload scene

Persistent GameObject Between scene Without Serilization

Hello, Here my purpuse : I want to keep GameObject in a Dictionnary.

Problem : When I changed scene my GameObject in my Dictionnary become null.

What I tried : Clone / Duplicate / Instantiate - And DontDestroyOnLoad (but just cloning objects in other scene. If then I destroy them, GameObject will be null too).

MapController : Save, Load, GameObject with Tag MapController

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class MapController : MonoBehaviour {
 
     private GameObject[] respawns;
     private GameObject[] destroy;
     string currentSceneName;
 
     public static MapController instanceRef;
 
     // Singleton for change Scene
     void Awake()
     {
         Debug.Log("Awake");
         if (instanceRef == null)
         {
             instanceRef = this;
             DontDestroyOnLoad(gameObject);
         }
         else
         {
             DestroyImmediate(gameObject);
         }
     }
 
     // LoadData -- When player Enter in NewScene
     void OnSceneLoaded(Scene scene, LoadSceneMode mode)
     {
         currentSceneName = SceneManager.GetActiveScene().name;
         Debug.Log("currentSceneName " + currentSceneName);
         if (MapDatabase.Instance.getDictionnaryDB().ContainsKey(currentSceneName))
         {
             respawns = MapDatabase.Instance.getObjectsByKey(currentSceneName);
             DestroySceneObject();
             LoadDataDB();
           
         }
     }
 
     // Call When just before SceneLoader (other class)
     public void CommitDataDB()
     {
         Debug.Log("CommitDataDB ");
         respawns = GameObject.FindGameObjectsWithTag("MapController");
         // foreach (GameObject respawn in respawns)
         // {
             // Debug.Log("CommitDataDB respawn " + respawn);
             // DontDestroyOnLoad(respawn);
         // }
         Debug.Log("currentSceneName CommitDataDB" + currentSceneName);
         MapDatabase.Instance.setObjectsByKey(SceneManager.GetActiveScene().name, respawns);
     }
 
     // Want to destroy object already in scene
     public void DestroySceneObject()
     {
         destroy = GameObject.FindGameObjectsWithTag("MapController");
         foreach(GameObject d in destroy)
         {
             Destroy(d);
         }
     }
 
     // Load Data when enter in new scene
     public void LoadDataDB()
     {
         foreach (GameObject respawn in respawns)
         {
             Debug.Log("respawn " + respawn);
             Instantiate(respawn, respawn.transform.position, respawn.transform.rotation);
         }
     }
 
     // Event for SceneLoad
     void OnEnable()
     {
         Debug.Log("OnEnable called");
         SceneManager.sceneLoaded += OnSceneLoaded;
     }
 
     // Just Disable Event when finished
     void OnDisable()
     {
         Debug.Log("OnDisable");
         SceneManager.sceneLoaded -= OnSceneLoaded;
     }
 }


MapDatabase : Dictionnary, store information.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MapDatabase {
 
     private static readonly MapDatabase instance = new MapDatabase();
 
     Dictionary<string, GameObject[]> dictionnaryDB = new Dictionary<string, GameObject[]>();
 
     static MapDatabase()
     {
 
     }
 
     public static MapDatabase Instance
     {
         get
         {
             return instance;
         }
     }
 
     public Dictionary<string, GameObject[]> getDictionnaryDB()
     {
         return dictionnaryDB;
     }
 
     public GameObject[] getObjectsByKey(string key)
     {
         return dictionnaryDB[key];
     }
 
     public void setObjectsByKey(string key, GameObject[] respawn)
     {
         dictionnaryDB[key] = respawn;
     }
 }
 

Comment

People who like this

0 Show 1
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 OlivierROGER · Apr 28, 2018 at 03:17 PM 0
Share

If you find a better way (optimization) to just DestroyAll then LoadAll (I mean go for Delta Object) i'm interest too. But I think if i do Observable or Loop over Object for controle if Object is still in same position, angle, etc ... This cost time too

1 Reply

  • Sort: 
avatar image

Answer by OlivierROGER · May 04, 2018 at 01:02 PM

Ok I found a solution with InactiveObject.

Not the best solutions (waiting for serialization methods)

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

124 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

DontDestroyOnLoad for a specific scene 1 Answer

Destroy Object when scene loads 1 Answer

DontDestroyOnLoad Slowing Loading Down 0 Answers

game object is not being destroyed 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges