Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by piratecody · Dec 17, 2020 at 10:31 AM · spawningscene-switchinglevelsscene-changelevel load

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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).

Inspector


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 uses HashMap (dictionary) and HashSet internally and not lists/arrays for faster key search times

  • Enemy.cs implements OnValidate in such a so you can easily generate unique IDs by clearing ID field in the inspector (this is especially useful because duplicating enemy gameObject will duplicate it's IDs as well)


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

145 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges