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 Pixel3d1234 · Jan 06, 2021 at 08:25 AM · prefabdestroyvariablesif

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;
     }    
  
 }

Comment
Add comment · Show 2
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 Llama_w_2Ls · Jan 06, 2021 at 08:49 AM 0
Share

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

avatar image Pixel3d1234 Llama_w_2Ls · Jan 06, 2021 at 03:51 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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)

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
avatar image
0

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.

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
avatar image
0

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

Comment
Add comment · Show 1 · 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 Pixel3d1234 · Jan 06, 2021 at 03:26 PM 0
Share

Thx for helping i'm gonna chack if its working.(:

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

153 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

Related Questions

How to spawn a 'boss' after all enemies defeated and then kill that 'boss'? 1 Answer

Destroy Prefab clone 2 Answers

Destroying Objects. 1 Answer

How to refer to a collision between clones of the same prefab? C# 1 Answer

Destroying an instantiated prefab particle effect 4 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