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 /
  • Help Room /
avatar image
0
Question by tuzi419711227 · Apr 22, 2021 at 01:10 PM · dontdestroyonloadpersistence

How do I destory the enemy and item so they don't respwan after reload the scene.

I am stuck on this for 2 days now, can anyone help me please. I am making a grid base dungeon explore game. The issue is when the player go to the next floor ( trigger next scene) and come back, all the enemies and items reappear in the scene. I was able to get my player stats working by using a static class, but I can't do that for the enemy or pickup because some of them share the same script. What I tried so far is to store each gameobject in the hierarchy into a list to get their name, then assign a bool list to get the current status. So right now I am able to get all the game object that tag with the enemy on the console, and it does output a debug message that tells if the enemy should be destroy on the next load. I tried to set active(false) of the list or destroy the gameobejct in the list, both don't work. What should I do next? Below is what my game looks like and script.

alt text

using UnityEngine;

using System.Collections;

using System.Collections.Generic;

public class destroyonload : MonoBehaviour { // Start is called before the first frame update public static destroyonload instance;

 public static List<GameObject> mylist = new List<GameObject>();
 List<GameObject> list1= new List<GameObject>();
 public static List<bool> boollist = new List<bool>();
 public static List<GameObject> itemlist;

 void Awake()
 {
     DontDestroyOnLoad(this);

     if(instance == null)
     {
         instance = this;
     }

      else
     {
       //Duplicate GameManager created every time the scene is loaded
       
       Destroy(gameObject);
     }       
     
     foreach (GameObject obj in Object.FindObjectsOfType(typeof(GameObject)))
         {     
             //obj.CompareTag("canbepickup")||
             if(obj.CompareTag("enemy")){
                 Debug.Log(obj.name);
                 list1.Add(obj);
                     if(Enemy.shoulddestroy == false){
                     Debug.Log("First time encounter" + obj);
                     boollist.Add(false);
                 }

                 else{
                     Debug.Log("the hit object should be destory" + obj);
                     boollist.Add(true);
                 }
                 //array2.AddRange(list1,boollist);
             }
         }

     itemlist.AddRange(list1);
 }

 void Start()
 {
     if(itemlist!= null)
     {
         foreach(GameObject list in mylist){
             Debug.Log("attempt to destory list" + list);

             if(list.activeInHierarchy){
             list.SetActive(false);
             Debug.Log("attempt to set active false");
             }

             Debug.Log("done destory");
         }
         
         int i = 0;
         while(itemlist[i]!= null){

                 int j =0;
                 for(j=i;j<itemlist.Count;j++)
                 {
                     if(boollist[j] ==true){
                         Destroy(itemlist[i]);
                     }
                 }
                 i=i+1;
         }
         
             
         
     }
     
     
 }

}

Enemy Script:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Enemy : Character {

 public static bool shoulddestroy= false;

 public int damageStrength;

 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.CompareTag("Player")){
         shoulddestroy = true;
         print("you should destory on next load"+ other.gameObject);
     }
         
 }

 void OnTriggerExit2D(Collider2D other)
 {
     if (other.gameObject.CompareTag("Player")){
         
         print("im out"+ other.gameObject);
     }
         
 }
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 GameDeveloperAf · Apr 22, 2021 at 02:29 PM

Do you mean you want to remove all item after reload scene? You can save your progress to save all progress. Fore example if you want to destroy and after reload the scene you do not want to see that item there then you need to save the progress.

Comment
Add comment · Show 4 · 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 tuzi419711227 · Apr 22, 2021 at 02:39 PM 0
Share

Sorry my English isn't good. But I want to destroy the enemy that the player already kill in the current level, so it doesn't reappear after reload the scene. This also apply to all the item that can be pick up, only destroy the item that player collected not the entire item in the level.

Also what you mean by the save progress, how do I do that. I have a static bool variable shoulddestory attach to all the enemy, this indicate if the enemy should be destroy when reload the scene. my question is how should I destroy it so it doesn't appear when reload the scene?

avatar image GameDeveloperAf tuzi419711227 · Apr 22, 2021 at 04:31 PM 0
Share

What language do you know maybe I know that language which you know. Anyway. You can save any of your game progress with PlayerPrefs, XML, Binary or Scriptable Objects. There are 4 type of save and load system in Unity. First of all you need to make a logic here to do that what you want. https://www.youtube.com/watch?v=-fAqBNeEWjs&list=PL5KbKbJ6Gf9_Rvhx1ye1iHxtLTsAaH7uD Take a look at youtube to learn PlayerPrefs to save your items and load

avatar image tuzi419711227 GameDeveloperAf · Apr 22, 2021 at 05:51 PM 0
Share

The issue I have is about destroy the object so it no longer exist in the scene after reload. I already have my player stats store correctly by using static class, and I use the scriptable objects to store the enemy stats, but this doesn't keep the object destroyed when reload. I watched the link about the playerprefs ,it only talk about storing data which is not what I want it at the moment.

The most common approach I find is to store each item with an id and a bool list so the script knows which ones you have to destroy. I tried this approach, but I was not able to destroy the gameobject on reload. If you are familiar with this, can you tell me what is missing in my script?

Show more comments

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

159 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 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

[2D] problem with character persistence and respawn (dontdestroyonload singleton) 0 Answers

Countdown Timer 2 Answers

DontDestroyOnLoad Error with Firebase Analytics 0 Answers

Player exits don't destroy on load because of moving platform 0 Answers

How can I destroy a GameObject after a scene is loaded? 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