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

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 ?

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.

2 Likes

Here’s my load/save treatment… check out how other tutorials do it:

Load/Save steps:

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 :

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class GenerateAutomaticGuid : Editor
{
    [MenuItem("GameObject/Generate Guid", false, 11)]
    private static void GenerateGuid()
    {
        foreach (GameObject o in Selection.objects)
        {
            o.AddComponent<GenerateGuid>();
            o.GetComponent<GenerateGuid>().GenerateGuidNum();
            o.tag = "My Unique ID";
        }
    }
}

In the end I have for example 13 gameobjects with this script attached to them :

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GenerateGuid : MonoBehaviour
{
    public string uniqueGuidID;

    private Guid guidID;
   
    public void GenerateGuidNum()
    {
        guidID = Guid.NewGuid();
        uniqueGuidID = guidID.ToString();
    }
}

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 :

objectsToSave = GameObject.FindGameObjectsWithTag("My Unique ID").ToList();

And a bit more changes that’s it.

Now I have on each saved gameobject unique id number that is not change.

2 Likes

Great solution!

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.