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 ShinyTaco · Sep 19, 2013 at 02:33 PM · gameobjectprefabdestroyhow

Why is an Instance of Prefab not being Destroyed?

Hey guys,

Not really sure what I'm doing wrong here. I have a prefab with some sounds and colliders on it. Everything works fine when I walk into it.

This prefab needs to be used several times.

When I walk into the first prefab, everything works great and goes as planned.

However, when I walk into the second prefab, everything works great. All the sounds play, etc, but for whatever reason the prefab does not get destroyed.

It just stays there, like nothing happened. I can continue to walk into the colliders and all the sounds will play, etc, but the prefab never gets destroyed?

What am I doing wrong and how do I fix this?

Here's my code:

      if (myTrigger.tag == "theTrigger"){
         audio.PlayOneShot(mySound);
         Destroy(myPrefab);
            print("prefab destroyed");
 

Any ideas?

Thanks in advance!

*Edit to add print("prefab Destroyed"); and prefab destroyed shows up so we know that Destroy(myPrefab); was called.

  • Edit Why isn't an instance of a prefab being destroyed?

Comment
Add comment · Show 3
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 vexe · Sep 19, 2013 at 02:35 PM 0
Share

Where's the ending closing curly bracket? I guess, you just forget it here...

avatar image ShinyTaco · Sep 19, 2013 at 02:41 PM 0
Share

No I didn't forget it, it didn't copy and paste over.

avatar image vexe · Sep 19, 2013 at 03:21 PM 0
Share

It's not accurate to say "destroy the prefab" - more accurate would be "destroy the gameobject that I instantiated from my prefabs"

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Eric5h5 · Sep 19, 2013 at 03:53 PM

You should destroy the GameObject instance in the scene, not the prefab.

Comment
Add comment · Show 6 · 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 ShinyTaco · Sep 19, 2013 at 03:56 PM 0
Share

Thanks. I am, myPrefab is the instance. You get an error if you try to destroy the prefab.

avatar image Eric5h5 · Sep 19, 2013 at 04:15 PM 0
Share

O$$anonymous$$, well, I'd recommend using better names, since it's quite confusing calling something a prefab if it's not actually a prefab.

avatar image clunk47 · Sep 19, 2013 at 04:54 PM 0
Share

Are you sure you're using an instance? Not sure if you're instantiating or you drag and drop your instances in the editor...

 public GameObject prefab;
 
 void YourFunction()
 {
     GameObject instance = (GameObject)Instantiate(prefab, position, rotation);
 }
avatar image ShinyTaco · Sep 20, 2013 at 01:19 AM 0
Share

I'm dragging and dropping the prefabs into the scene.

For example, if it was Super $$anonymous$$ario Brothers it would be 10 coins in the game scene.

I click and drag 10 of my prefabs and and want to collect them. I can get the first prefab and destroy it, but the others prefabs do not get destroyed.

I don't need to instantiate any of them, the prefabs are already layed out ahead of time.

Thanks for the help.

avatar image Eric5h5 · Sep 20, 2013 at 01:38 AM 1
Share

Those are still instances, not prefabs. Dragging a prefab into a scene instantiates it.

Show more comments
avatar image
0

Answer by ShinyTaco · Sep 20, 2013 at 02:54 AM

Finally figured it out. I used:

 Destroy(myTrigger.gameObject);

Instead of:

 Destroy(myPrefab);

Although, I'm still not clear on why you can't use Destroy(myPrefab); (which is an instance of a prefab, not the actual prefab).

Thanks for all who tried to help.

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 k014 · Oct 04, 2013 at 01:01 AM 0
Share

seems like you can not destroy monobehaviours, only gameobjects, glad to see your problem solved :)

avatar image Eric5h5 · Oct 04, 2013 at 01:09 AM 0
Share

@k014: You can destroy anything, not just GameObjects. As you can see in the docs, Destroy is a member of Object, not GameObject.

avatar image k014 · Oct 04, 2013 at 01:13 AM 0
Share

well everyday you learn something new :)

avatar image Xitech_ · Oct 04, 2013 at 09:26 AM 0
Share

I guess I should remember this when I might encounter this problem, glad its solved

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

19 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

Related Questions

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

Collision Detection for a Prefab? 3 Answers

PlayerRespawn class wont Instantiate the player prefab 1 Answer

Destroying a prefab on collision with a cube? 1 Answer

Cannot destroy child object in prefab- Error 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