Can anyone give an example how to make a unique ID for each enemy or pickup, and store in the ID in array when the enemy is killed or item picked up.

I am a newbie in unity, can anyone help me with an code example, please. I have watched many tutorials about save/load data persistence, but I still have no clue how to make sure the enemy is dead after reloading the scene. Suppose I have three types of enemy: Slime, Red Slime, and bat. Each enemy type has multiple copy and they all attach the same Enemy class script. How do I set a unique ID for each enemy gameobject, so the program knows which one is dead when the scene reload? Also, how do I destroy them so they don’t respawn when the scene reload?

public class Enemy : Character
{

public bool shoulddestroy= false;

public int damageStrength;

void OnTriggerEnter2D(Collider2D other)
{
    if (other.gameObject.CompareTag("Player")){
        shoulddestroy = true;

        print("you should destory on next load"+ other.gameObject);
    }
        
}

}

It will be a little confuse but in short time I found this logic.

First thing duplicate your all items in the prefabs folder one of them add this script “GameItem”
And to others add “SpawnedGameItem” script.

Create a new script and call it “GameItem” and then add this code into it. Add this “GameItem” script into your item objects.

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

public class GameItem : MonoBehaviour
{
    // We find GameProgress gameobject and it into "gameProgress".
    public GameProgress gameProgress;

    // We take "MyItem" class to add our data from here.
    public MyItem myItem;

    // We need to check is it destroyed or not.
    // You can access it from your player when you want to destroy it.
    public bool destroy;

    void Start()
    {
        // It's automatically finds and gets script of that gameobject.
        // Be sure add Tag into GameProgress gameobject and call that Tag as "GameProgress".
        gameProgress = GameObject.FindGameObjectWithTag("GameProgress").GetComponent<GameProgress>();

        // This will add position into that class.
        // Also you can store rotation, ID, textures ID and so on.
        myItem = new MyItem()
        {
            Position = transform.position
        };

        // We add our data into "MyItemDatabse" using "Add()".
        gameProgress.myItemDatabase.myItem.Add(myItem);
    }

    private void Update()
    {
        // You need to set true for "destroy" bool variable when you need to remove item from scene and saved file.
        // When the "destroy" is true then it will destroy and remove from saved XML file.
        // And it will not appear there when you reload the scene.
        if (destroy) 
        {
            // We remove our item from "MyItemDatabase" class using "Remove(myItem)".
            gameProgress.myItemDatabase.myItem.Remove(myItem);

            // Then we need to update our saved file.
            // And we save it again.
            gameProgress.Save();
            
            // Destroy our game object from scene.
            Destroy(gameObject);
        }
    }
}

Add this code into SpawnedGameItem script

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

public class SpawnedGameItem : MonoBehaviour
{
    // We find GameProgress gameobject and it into "gameProgress".
    public GameProgress gameProgress;

    // We take "MyItem" class to add our data from here.
    public MyItem myItem;

    // We need to check is it destroyed or not.
    public bool destroy;

    void Start()
    {
        // It's automatically finds and gets script of that gameobject.
        // Be sure add Tag into GameProgress gameobject and call that Tag as "GameProgress".
        gameProgress = GameObject.FindGameObjectWithTag("GameProgress").GetComponent<GameProgress>();
    }

    private void Update()
    {
        // You need to set true for "destroy" bool variable when you need to remove item from scene and saved file.
        // When the "destroy" is true then it will destroy and remove from saved XML file.
        // And it will not appear there when you reload the scene.
        if (destroy)
        {
            // We remove our item from "MyItemDatabase" class using "Remove(myItem)".
            gameProgress.myItemDatabase.myItem.Remove(myItem);

            // Then we need to update our saved file.
            // And we save it again.
            gameProgress.Save();

            // Destroy our game object from scene.
            Destroy(gameObject);
        }
    }
}

And then create another new script and call it “GameProgress”. And Create a new Empty GameObject in the scene and call it “GameProgress” and add “GameProgress” tag into that GameProgress GameObject and then add “GameProgress” script into that GameProgress GameObject. Add the following code into “GameProgress” script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
using System.Xml.Serialization;

[System.Serializable]
public class MyItem
{
    // You can store here what you want.
    public Vector3 Position;
}

[System.Serializable]
public class MyItemDatabase
{
    // We need to store more then one class.
    // And because of this we need to convert our "MyItem" class into "List<>".
    [XmlArray("Items")]
    public List<MyItem> myItem;
}

public class GameProgress : MonoBehaviour
{
    // Your item object.
    public GameObject itemObject;

    // To save, load and remove each Level progress into different XML files using unique ID for it.
    // You need give "ID" to each level.
    public int ID;

    // We need to reach to "MyItemDatabase" class from "GameItem" script.
    public MyItemDatabase myItemDatabase;

    private void Start()
    {
        // Load file.
        if (System.IO.File.Exists(Application.dataPath + "/StreamingAssets/Level.xml" + ID))
        {
            Load();

            for (int i = 0; i < myItemDatabase.myItem.Count; i++)
            {
                // If (Player did not take some item objects) there are item objects in the XML file then we need to spawn our item objects into scene.
                // We take our position of our item objects and take that item objects into scene back.
                Instantiate(itemObject, myItemDatabase.myItem*.Position, Quaternion.identity);*

}
}
}

private void Update()
{
Save();
}

private void Load()
{
// We check is there XML file exists or not before we load it.
if (System.IO.File.Exists(Application.dataPath + “/StreamingAssets/Level.xml” + ID))
{
// If is there XML file then we load it.
XmlSerializer XmlSerializer = new XmlSerializer(typeof(MyItemDatabase));
FileStream FileStream = new FileStream(Application.dataPath + “/StreamingAssets/Level.xml” + ID, FileMode.Open);
myItemDatabase = XmlSerializer.Deserialize(FileStream) as MyItemDatabase;
FileStream.Close();
}
}

public void Save()
{
// We save our progress.
XmlSerializer XmlSerializer = new XmlSerializer(typeof(MyItemDatabase));
FileStream FileStream = new FileStream(Application.dataPath + “/StreamingAssets/Level.xml” + ID, FileMode.Create);
XmlSerializer.Serialize(FileStream, myItemDatabase);
FileStream.Close();
}
}
In your player script make “destroy” bool variable to true when you want to destroy the item.
After doing all these create a new folder in the Assets folder and call it StreamingAssets to save all our data into it That’s all