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.
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);
}
}
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.
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?
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
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?
Your answer
![](https://koobas.hobune.stream/wayback/20220613032657im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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