Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
13
Question by ina · Dec 27, 2010 at 04:11 AM · gameobjectinstantiateobjectdestroy

Destroy the current GameObject?

How do you destroy the current GameObject? (the one the script is in)

Destroy(GameObject) doesn't work nor does Destroy(this) ... The latter seems to destroy the construct, but does not remove the actual GameObject onscreen...

When using Destroy(gameObject), it seems I am no longer able to instantiate new ones if I kill off the only prefab on screen (even from another script).

BTW - what is the difference between GameObject and gameObject and this?

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 Mike 3 · Dec 27, 2010 at 04:27 AM 2
Share

GameObject is the GameObject class, gameObject is a property which retrieves the GameObject attached to the script, this is an instance of the script

avatar image ina · Dec 27, 2010 at 04:34 AM 1
Share

This question is probably better broken down into different topics... http://answers.unity3d.com/questions/33473/destroy-the-first-instanced-prefab-while-still-being-able-to-generate-more-prefab and http://answers.unity3d.com/questions/33474/difference-between-gameobject-and-gameobject-and-a-scene-item-named-gameobject

avatar image ina · Dec 27, 2010 at 06:22 PM 1
Share

huh.. why the downvote

7 Replies

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

Answer by _Petroz · Dec 27, 2010 at 06:30 AM

Destroy(gameObject) is the correct way to the GameObject your script is attached to.

When using Destroy(gameObject), it seems I am no longer able to instantiate new ones if I kill off the only prefab on screen (even from another script).

I'm not sure what you mean by 'on screen'. Prefabs do not exist inside the scene, they are in the project. You should not be destroying Prefabs.

what is the difference between GameObject and gameObject and this?

GameObject is a class.

gameObject is a property you can call on a MonoBehaviour to the the GameObject instance that Monobehaviour instance is attached to.

this is the instance of the class the code is in.

Comment
Add comment · Show 2 · 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 ina · Dec 27, 2010 at 06:23 PM 0
Share

okay... do you need a prefab in the scene to instantiate it?

avatar image tubelightboy ina · Jul 04, 2021 at 07:39 PM 0
Share

prefab is just a reusable game object that once you create in unity and if you create it as a prefab, all the properties of the gameobject are saved and it can be reused in multiple scenes as it is.

avatar image
13

Answer by B-rad · Nov 07, 2012 at 08:06 PM

Have you tried: Destroy(this.gameObject);

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 anilo · Jan 20, 2014 at 05:16 AM 0
Share

this worked for me.

avatar image ciso · May 05, 2014 at 03:55 AM 0
Share

this worked for me to!

avatar image Milgras · Apr 20, 2016 at 03:56 PM 0
Share

this worked for me to. Thank you!

avatar image Ryan_Cori · Apr 20, 2016 at 05:10 PM 0
Share

This should be the same as Destroy(gameObject) though right?

avatar image
3

Answer by Jesus_Freak · Dec 27, 2010 at 04:17 AM

Destroy(gameObject) does (should) gt rid of attached gameobject

From a diff object you could say

Destroy(gameObject.FindWithTag("tag"));

Or something like that.

Comment
Add comment · Show 3 · 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 ina · Dec 27, 2010 at 04:23 AM 0
Share

If you have just 1 prefab onscreen, that seems to get rid of all instances though - so that you can't call Instantiate on it again from another script

avatar image Mike 3 · Dec 27, 2010 at 04:33 AM 0
Share

It gets rid of a single instance, the one the script is attached to (it's actually Component.gameObject in the base class of your script). If all of your objects are dying, you're calling it on different objects

avatar image ina · Dec 27, 2010 at 04:42 AM 0
Share

how do you call it on a different object?

avatar image
0

Answer by Unityart3dsMAX · Jan 07, 2013 at 07:21 PM

Ok here you go

This script goes with the gameobject that is Instantiating the gameobject.

var bullet : GameObject;

var backup : GameObject;

function Update () { if(Input.GetKey("a")) backup = Instantiate(bullet, transform.position, transform.rotation);

}

The bak up file will make it to were if the gameobject gets destroyed then it will still be able to be Instantiated.

Im running out of time bu if you want to know more simple ways Email me

Email : unityart3ds@gmail.com

I will explain the simplest way to do it cause it is simple

So pleas Email me... :)(: ...

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 sriram90 · Dec 27, 2010 at 06:32 AM

this gameobject is nothing but in which gameobject you have placed that script...if you use Destroy(this.gameObject);

it'll destroy the gameobject in which you have placed the script....

Destroy(gameObject)- it'll destroy particular gameobject what you have specified.

try to store instantiated gameobject name as "clone" and destroy that clone object using Destroy(clone); it'll destroy all objects from origin.

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 ina · Dec 27, 2010 at 07:07 AM 0
Share

that only works if you have one instantiated objects - what if you have more than one

avatar image sriram90 · Dec 27, 2010 at 07:17 AM 0
Share

actulally are you creating multiple objects in a same time?? can you give more explain about your concept? and what you're looking for??

avatar image sriram90 · Dec 27, 2010 at 07:18 AM 0
Share

or you want to destroy objects one by one using time?

avatar image sriram90 · Dec 27, 2010 at 07:40 AM 0
Share

why cant you try array....store that into "clone[i]" and Destroy(clone[i]); if you want delete all instantiated prefabs....

avatar image ina · Dec 27, 2010 at 06:24 PM 0
Share

okay so loop through the entire array - no way to just set clone=Array(); - 1 line delete all?

Show more comments
  • 1
  • 2
  • ›

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

13 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

Related Questions

Cannot destroy child object in prefab- Error 1 Answer

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

Cannot cast from source type to destination type 1 Answer

PlayerRespawn class wont Instantiate the player prefab 1 Answer

Replace GameObject vs. replacing mesh and material? 2 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