Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How can I keep the objects in scene instance id's after loading the scene again?

Discussion in 'Editor & General Support' started by SharonL75, Oct 7, 2020.

  1. SharonL75

    SharonL75

    Joined:
    Aug 13, 2020
    Posts:
    91
    I created a save/load system and the system writing to a json file on the hard disk the saved objects instance id.

    The problem is in my game to load a saved game you have to click the escape key back to the main menu so it's loading the main menu scene then I click the load button and it's loading again the Game scene with all the game objects but now the objects have a different instance id's. In fact each time I will make a load game it will give the objects a different instance id's and then the instance id/s in the save file will be always different from the new given instance id's when loading the Game scene again.

    Any logic solution for that problem ?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,890
    Don't try to save and reload Unity instanceIDs. That is essentially an internal identifier for Unity. It isn't really something you should be integrating into your save system. If you need a unique identifier for your objects, create your own ID field.
     
    Joe-Censored and SharonL75 like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
  4. SharonL75

    SharonL75

    Joined:
    Aug 13, 2020
    Posts:
    91
    I did it my save load system is ready and working. What I did in the end is a small mono script with menu item so when you select objects in the hierarchy and then make right click in the menu you select Generate Guid.

    The script automatic add to the selected gameobjects a small script with public string that generate a new Guid.
    And also add a tag to the selected gameobjects.

    This script add another small script and generate new guid :

    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEditor;
    6. using UnityEngine;
    7.  
    8. public class GenerateAutomaticGuid : Editor
    9. {
    10.     [MenuItem("GameObject/Generate Guid", false, 11)]
    11.     private static void GenerateGuid()
    12.     {
    13.         foreach (GameObject o in Selection.objects)
    14.         {
    15.             o.AddComponent<GenerateGuid>();
    16.             o.GetComponent<GenerateGuid>().GenerateGuidNum();
    17.             o.tag = "My Unique ID";
    18.         }
    19.     }
    20. }
    21.  
    In the end I have for example 13 gameobjects with this script attached to them :

    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class GenerateGuid : MonoBehaviour
    8. {
    9.     public string uniqueGuidID;
    10.  
    11.     private Guid guidID;
    12.    
    13.     public void GenerateGuidNum()
    14.     {
    15.         guidID = Guid.NewGuid();
    16.         uniqueGuidID = guidID.ToString();
    17.     }
    18. }
    19.  
    So now the selected gameobjects are tagged as "My Unique ID" and also have attached the GenerateGuid script.
    Then in my save load system I find the objects with FindByTags :

    Code (csharp):
    1.  
    2. objectsToSave = GameObject.FindGameObjectsWithTag("My Unique ID").ToList();
    3.  
    And a bit more changes that's it.

    Now I have on each saved gameobject unique id number that is not change.
     
    Epsilone and gabearts like this.
  5. gabearts

    gabearts

    Joined:
    Jun 30, 2014
    Posts:
    10
    Great solution!
     
  6. mms_bc

    mms_bc

    Joined:
    Aug 11, 2020
    Posts:
    1
    I tried your solution, but it did not work for me. I think the reason for that is, that I use prefabs for my gameobjects. Every time I close the editor or change the scene, the unique GenerateGuid Script is removed automatically by Unity.