• 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 robinsail · Feb 03, 2020 at 03:05 PM · destroyprefab-instance

Destroy not working on Prefab

I have an elevator in a scene that has only one floor. If someone presses button 2, when the doors open, I want the floor to look different from when the doors open on the 3rd floor. So, I've placed the furniture for each floor in its own prefab and I call up the prefabs as indicated below.

The instantiations are working fine. However, if I select floor 3 after I've been to floor 1, floor 3 prefab appears but floor 1 prefab doesn't go away. It looks like Destroy isn't working. And I get an error message: Destroying assets is not permitted to avoid data loss. If you really want to remove an asset use DestroyImmediate (theObject, true).

In reading about DestroyImmediate, it doesn't look like what I need.

     switch (currFloor)
     {
         case "01":
             Instantiate(floorPrefab[0], new Vector3(-0.221f, 0f, -0.286f), Quaternion.identity);
             Destroy(floorPrefab[1]);
             Destroy(floorPrefab[2]);
             Destroy(floorPrefab[3]);
             Debug.Log("floor1");
             break;
         case "02":
             Instantiate(floorPrefab[1], new Vector3(-0.221f, 0f, -0.286f), Quaternion.identity);
             Destroy(floorPrefab[0]);
             Destroy(floorPrefab[2]);
             Destroy(floorPrefab[3]);
             Debug.Log("floor2");
             break;
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

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Bunny83 · Feb 03, 2020 at 03:42 PM

You are one of those who confuse a "prefab" with an "instance of a prefab". You try to actually destroy the prefab itself. The thing that lives in your project. What you want to destroy is the instance of your prefab that lives in your scene. Note that the Instantiate method returns the instance it creates. It does nothing to the source object you pass in. The instance also has no relation to the object it was cloned from.


If you want to manage your instantiated floor objects you have to store the reference to the instantiated object somewhere.


To me it looks like that at any time you only want to have a single floor object in your scene. If that's the case you can simply use a single GameObject variable inside your class like this:

 private GameObject currentFloorObj = null;

in your instantiate code you would first destroy the old one, if there is one and then create the new one:

 case "01":
     if (currentFloorObj != null)
         Destroy(currentFloorObj);
     currentFloorObj = Instantiate(floorPrefab[0], new Vector3(-0.221f, 0f, -0.286f), Quaternion.identity);
     break;

Note that your switch case construct looks like a horrible approach. Why is "currFloor" a string? Since you have your different floor prefabs already in an array, why not using an int variable? That way you can get rid of the whole switch case thing. Just create another array with your spawn positions or group the prefab with your spawn position together in a custom serializable class. We don't know what logic is behind your code. However you should learn how to seperate logic from configuration data. In general avoid any hardcoded values inside your code. If you do something similar more than twice, it's most likely you could simplify your code into a loop / array / method / ... We don't see the whole picture of your project so we can not tell you what you could do.

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
1

Answer by LeFlop2001 · Feb 03, 2020 at 03:13 PM

You should disable instead of destroying them.

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 robinsail · Feb 03, 2020 at 04:22 PM

This is very helpful. and it explains why my prefab information disappeared in Inspector after running Play. :-)
I used a string because the currFloor variable is also being used by another method in the script to change the floor number that is in a TextMeshpro text field. I'm using switch because I want to add some additional code to the cases which will be unique for each floor. For example, the sound of the elevator moving will run longer for floor 8 than for floor 2. Based on your feedback though, I'm reexamining how I have this set up and I'm reworking it. Thanks!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

128 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Delete a specific instantiated prefab by its vector3 coordinates? 0 Answers

GameObject not being Destroyed (I've Searched...!) 2 Answers

Need assistance with destroying Gameobjects 1 Answer

Experiencing NullReferenceException only in build with Coroutine 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