- Home /
If i destroyed the prefab it won't destroy again. I have no idee how to fix, please help!
public int Plives; //the lives of player
public GameObject Heart2; //all the prefabs
public GameObject Heart3;
public GameObject Heart1;
public Transform Hr1; //respawnpoint of the lives
public Transform Hr2;
public Camera cam; //the camera
private void Update() //if this has been already run i doesn't work again.
{
if(Plives == 2)
{
Destroy(Heart1);
}
if(Plives == 1) // same for this one
{
Destroy(Heart2);
}
if (Plives == 0) //This one works because it doesn't need to be called twice.
{
Destroy(Heart3);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 0);
}
if (Plives < 0) //This was created to patch a bug so the lives wont go in negative numbers.
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 0);
}
}
public void Setlive1()
{
if(Plives == 2) //This is the script to make new harts and then parent them to tehe camera.
{
var Lives1 = Instantiate(Heart3, Hr1.position, Quaternion.identity);
Lives1.transform.parent = cam.transform;
Plives += 1;
}
if(Plives == 1)
{
var Lives2 = Instantiate(Heart3, Hr2.position, Quaternion.identity);
Lives2.transform.parent = cam.transform;
Plives += 1;
}
}
The reason you can't destroy it again is because there is no reference to the object to destroy, because it has been destroyed. You should never try to destroy prefabs, scene objects and instantiated clones of a prefab are perfectly reasonable to destroy, since there is no chance of a loss of data about that object. You should instantiate a clone of the scene object and destroy the clone, not the original. Hope that makes sense @Pixel3d1234
I don't clone the originals i clone the last hart cause that one will never be destroyed and if it gets destroyed the scene has reloaded.
Answer by julianhahn01 · Jan 06, 2021 at 01:51 PM
If I read your Code right, your Heart1... are the prefabs saved in your assets and assigned through your inspector. In your Setlive method you instanciate your prefab. When you want to destroy an object from your scene, you need to destroy the instance, not the asset. In your case: you want to destroy "lives1" not the saved asset "Heart1"
there should be an error in the console which says "your are trying to delete an prefeb (assets) this is not allowed)
Answer by julianhahn01 · Jan 06, 2021 at 09:12 AM
Your code trys to destroy the prefab asset. You want to destroy the instance of that object! In this case, your Lives1 and Lives2! Also, try a list of sprites or gameObjects for you feature.
Answer by Mickeldebs · Jan 06, 2021 at 12:45 PM
add 2 new private gameobjects into your script:
private GameObject Heart3Inst;
private GameObject Heart2Inst;
and then add a start function then instantiate your prefabs in there like this:
private void Start()
{
Heart3Inst = Instantiate(Heart3, Hr3.position, Quaternion.identity);
Heart2Inst = Instantiate(Heart2, Hr2.position, Quaternion.identity);
}
and then when you call destroy you need to destroy the instantiation not the prefab:
Destroy(Heart3Inst);
Destroy(Heart2Inst);
that way you don't destroy your original prefabs you only destroy your instantiated ones
and adding to julians answer you can remove the sprites on your gameobjects and then adding them again when adding a life or even decrease the alpha value to 0 and then to 1 when adding them again
hope this helps