How To Save which enemies are dead

So I want to save which enemies are dead when i click a save button, I already got a script to save my position when i click the button using PlayerPrefs, and I have tried using it for saving which enemies are dead but it just saves every enemy as dead ofcourse.

So I have no idea how to go about this and how to even make it work, what kind of alglorith should I use and help would be very much appretiated.

Thanks.

If you have an enemy class, or something like that, then you could give it a boolean, to check if they are dead or not

If you want to continue to use PlayerPrefs, you’ll have to give every enemy a unique ID and use that in the key you send to PlayerPrefs.

That said, writing your own data save/load code will be much more useful and extensible - I recommend you give this tutorial a whirl.

Could you please expplain the idea you just gave me with the enemyID more detailed? I have an enemy class yes btw.

Well, you add a field - could be a string or int - that holds a unique value for every enemy. You can set this manually, or by script (e.g. in an editor script use FindObjectOfType(), loop through it, and set each one to the index, or maybe generate it based on the object’s position).

Once you have that, you set PlayerPrefs like you would with the player. but instead of PlayerPrefs.SetInt(“PlayerBlahBlah”, 1), you use PlayerPrefs.SetInt(“EnemyDead_”+yourEnemyID, 1).

1 Like

Alright I understand everything atm but one more thing… How do i link the string with the game object? do i have to do it manually like if(PlayerPrefs.GetInt(“EnemyDead”+EnemyID") == 1){
DestroyGameObject;
}
How does it know which one to destroy?

If you imagine what it’s doing there:
When you call PlayerPrefs.SetInt(“EnemyDead”+enemyID), the PlayerPrefs functions will only see PlayerPrefs.SetInt(“EnemyDead27”) - it’ll go off, find the slot for EnemyDead27, and save the value there.

So now when you call PlayerPrefs.GetInt(“EnemyDead”+enemyID), it goes off and finds EnemyDead27, and sends back the value it finds.

So for every unique enemyID value that exists, it’ll be going to a different slot. It sees EnemyDead11, EnemyDead23, and EnemyDead36 and three unique, unrelated “slots” in which to put information.

So, I made enemyID to be the starting position on the z-axis. Now on my regular save and load buttons I attached two separate functions when you click them:
public void EnemySave(){
PlayerPrefs.SetInt(“Enemy_Is_Dead_”+EnemyID,SaveIfDead); //SaveIfDead is an integer, if the enemy’s health is lesser than zero it becomes 0, else it becomes 1.
}
public void EnemyLoad(){
die = PlayerPrefs.GetInt(“Enemy_Is_Dead_”+EnemyID);
}
void Update(){
if(die == 1){ //die is an integer, since playerprefs cant save booleans
Destroy(gameObject);
}

}
I know I am doing something wrong because it only works for the first enemy GO that is instanciated “Enemy” , for “Enemy1,2,3… etc” it doesn’t work.

bump