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
0
Question by mak431020 · Jan 25, 2021 at 02:08 PM · save game

How to save the destroyed state of an enemy ?

On Trigger enter the enemy get destroyed but every time I play the game the enemy get back. How to save the destroyed state of enemy so that the enemy wont get back ?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Llama_w_2Ls · Jan 25, 2021 at 02:31 PM

You would have to save a bool that contains data on whether the enemy was destroyed or not. You could try using PlayerPrefs for this. For example:

 const string EnemySaveData = "WasDestroyed";

     private void OnTriggerEnter(Collider other)
     {
         // Enemy was destroyed
         Destroy(other.gameObject);
 
         // Save this info
         PlayerPrefs.SetString(EnemySaveData, "true");
     }

And when you first hit play, you need to check if the enemy was destroyed last time:

     private void Start()
     {
         string WasDestroyed = PlayerPrefs.GetString(EnemySaveData);
 
         // Destroys enemy if they were destroyed
         // previously
         if (WasDestroyed == "true")
             Destroy(gameObject);
     }

@mak431020

Comment
Add comment · Show 5 · 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
avatar image mak431020 · Jan 26, 2021 at 06:10 AM 0
Share

it works but it destroy all enemy. I destroyed only one enemy but when I play again it destroyed all enemy.

avatar image Llama_w_2Ls mak431020 · Jan 26, 2021 at 07:51 AM 0
Share

Here's another way you could do it using PlayerPrefs then. You could save the name of the enemy as the key. That way, as long as all your enemies are named differently, it should only destroy the enemies that were destroyed last time, and not all of them. For example:

 private void OnTriggerEnter(Collider other)
 {
          // Enemy was destroyed
          Destroy(other.gameObject);
  
          // Save this info
          PlayerPrefs.SetString(other.gameObject.name, "true");
 }

You'll also need a reference to all your enemies, either by referencing the parent of all the enemies, or all of them individually. I'm going to use the parent, which assumes that all enemies live under one empty gameobject called Parent:

 private void Start()
 {
      GameObject Parent = GameObject.Find("Parent");
      Transform[] enemies = Parent.GetComponentsInChildren<Transform>();
 
      foreach (Transform enemy in enemies)
      {
           // if they are an enemy
           if (!enemy.gameObject.CompareTag("Enemy");
                continue;
 
           string WasDestroyed = PlayerPrefs.GetString(enemy.gameObject.name);
 
           if (WasDestroyed == "true")
                Destroy(enemy.gameObject);
      }
 }

@mak431020

avatar image mak431020 Llama_w_2Ls · Jan 26, 2021 at 08:32 AM 0
Share

Thank you so much

Show more comments
avatar image
0

Answer by wolfgraphicsLLC · Jan 25, 2021 at 02:37 PM

what do you man as in another enemy spawns ? as it is impossible for the same enemy that is destroyed to just still be there it would be done in the spawn manager that is spawning the clones of a prefab in the scene and as such is where you would limit the spawning.

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

111 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

Related Questions

Save a game code? 1 Answer

Scene Saving In Between Runtimes 1 Answer

Reading/Writing to a text file on non-windows platforms 1 Answer

How do I save a List in my project? 0 Answers

Close application from app list android event 1 Answer


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