- Home /
Save and Load Instantiate Enemies
In my game i have a button to save all instantiated enemies their hp, position or if they are alive/dead and In main menu is load button to go to stage scene and load enemies.
For example: There is three enemies sent out by Enemy Manager. I killed two of them and i pause it and save the game. Now i go back to main menu and load it. It should be one enemy instead of three.
Im also wondering is playerpref good in this case?
Answer by RendrWyre · Jan 01, 2020 at 10:14 PM
Player prefs doesn't really work if you don't want the player to be able to change the settings. For example, the player could save the game, edit the total enemies to 0, load the game and get access to the next level.
https://learn.unity.com/tutorial/persistence-saving-and-loading-data
That's a tutorial by Unity on data persistence between sessions using a binary file. It's a bit old (2014) but still works fine as the technique is basically the same (using a binary formatter to serilaize data to a binary file.. then deserializing the file and loading the data)
Answer by Magso · Jan 01, 2020 at 09:59 PM
Playerprefs would work if the enemies are instantiated using a for loop with an array of spawn points.
for(int i = 0; i < PlayerPrefs.GetInt("howManyEnemiesAreLeft", 0); i++)
{
Instantiate(enemy, point[i].position, point[i].rotation);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Is this a good way of resetting stats? 1 Answer
Need help with using Player Prefs to save number of coins collected. 1 Answer