- Home /
How to prevent enemies from respawning when reloading a level
I'm looking to add multiple levels to my game and I've got the transition working, for the most part. My main issue right now is that when I go back to the previous level, all of the enemies and collectibles respawn. Is there any way to prevent this or some kind of best practices for managing levels?
Answer by andrew-lukasik · Dec 17, 2020 at 10:53 PM
Create something that stores persistent session data. Then write/read data from it, like this:
using UnityEngine;
public class Enemy : MonoBehaviour, Persistence.IMember
{
[SerializeField] string ID = null;
string Persistence.IMember.ID { get=>ID; set=>ID=value; }
string _key_disabled, _key_ammo, _key_position;
[SerializeField] bool _persistent = false;
bool Persistence.IMember.persistent { get=>_persistent; set=>_persistent=value; }
[SerializeField] int _numAmmo = 100;
#if UNITY_EDITOR
void OnValidate () => Persistence.ValidateID( this );
void OnDrawGizmosSelected ()
{
if( _persistent )
UnityEditor.Handles.Label( transform.position , ID );
}
#endif
void Awake ()
{
Persistence.ValidateID( this );
if( _persistent )
{
_key_disabled = $"{ID}.disabled";
if( Persistence.IsDisabled(_key_disabled) )
gameObject.SetActive(false);
_key_ammo = $"{ID}.ammo";
if( Persistence.GetInteger(_key_ammo,out int getNumAmmo) )
_numAmmo = getNumAmmo;
_key_position = $"{ID}.position";
if( Persistence.GetVector(_key_position, out Vector3 getPos) )
transform.position = getPos;
}
}
void OnDestroy ()
{
// just my guess: scene changed or game unloaded, lets save this guy position:
if( _persistent && Persistence.IsEnabled(_key_disabled) )
Persistence.SetVector( _key_position , transform.position );
}
void OnDeath ()
{
if( _persistent )
{
// flag as disabled:
Persistence.SetDisabled( _key_disabled );
// remove data thats no longer useful/valid:
Persistence.RemoveVector( _key_position );
Persistence.RemoveInteger( _key_ammo );
}
}
void OnResurrected ()
{
// remove from disabled:
if( _persistent ) Persistence.SetEnabled( _key_disabled );
}
void OnAttack ()
{
if( _persistent ) Persistence.SetInteger( _key_ammo , _numAmmo );
}
}
Source code for Persistence.cs
Idea here is to attach unique ID strings to your Component
(and specific variables too) so they can be identified across time and different scenes. And then read and write values to a dedicated storage where those IDs match specific data entries about gameObject state (like is it enabled, it's position or ammo count).
Note that Persistence.cs
contains utilities to Save/Load game state already and custom EditorWindow to inspect session data (Game/Persistent Data/Inspector menu).
additional notes:
IDs don't have to be unique... necessarily. Many objects can source the same data too - it's just not the most common case. For example: boss enemy appearing across multiple scenes as different gameObjects but with identical ID could share health number or hidden gameplay stats.
You can use
Persistence.SetInteger(key,value)
(etc.) methods to store any data you need (health, score or whatever)Persistence
usesHashMap
(dictionary) andHashSet
internally and not lists/arrays for faster key search timesEnemy.cs
implementsOnValidate
in such a so you can easily generate unique IDs by clearing ID field in the inspector (this is especially useful because duplicating enemygameObject
will duplicate it's IDs as well)
Your answer
Follow this Question
Related Questions
Next Level to unlock when first level is completed 2 Answers
where to use scenes in a game like this ? 1 Answer
Remember scene settings when returning to scene 2 Answers
Unet Changing online scenes not working. 1 Answer
Scene loading problem 5 Answers