x


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?

more ▼

asked Dec 27 '10 at 04:11 AM

ina gravatar image

ina
3.3k 492 550 602

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

Dec 27 '10 at 04:27 AM Mike 3

huh.. why the downvote

Dec 27 '10 at 06:22 PM ina
(comments are locked)
10|3000 characters needed characters left

6 answers: sort voted first

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

From a diff object you could say

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

Or something like that.

more ▼

answered Dec 27 '10 at 04:17 AM

Jesus_Freak gravatar image

Jesus_Freak
1.2k 38 44 59

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

Dec 27 '10 at 04:23 AM ina

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

Dec 27 '10 at 04:33 AM Mike 3

how do you call it on a different object?

Dec 27 '10 at 04:42 AM ina
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Dec 27 '10 at 06:30 AM

_Petroz gravatar image

_Petroz
3.6k 27 35 57

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

Dec 27 '10 at 06:23 PM ina
(comments are locked)
10|3000 characters needed characters left

Have you tried: Destroy(this.gameObject);

more ▼

answered Nov 07 '12 at 08:06 PM

B-rad gravatar image

B-rad
1

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Dec 27 '10 at 06:32 AM

sriram90 gravatar image

sriram90
460 33 37 47

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

Dec 27 '10 at 07:07 AM ina

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

Dec 27 '10 at 07:17 AM sriram90

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

Dec 27 '10 at 07:18 AM sriram90

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

Dec 27 '10 at 07:40 AM sriram90

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

Dec 27 '10 at 06:24 PM ina
(comments are locked)
10|3000 characters needed characters left

once you have destroyed it how can you make it respawn in for instance i have a crosshair that when you press the button to aim the crosshairs are destroyed but how do i get them to come back when you get that button up?

more ▼

answered Jan 07 at 04:59 PM

majordillow gravatar image

majordillow
0

This unreadable punctuationless sentence is a comment, not an answer.

Jan 07 at 08:32 PM Loius
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2090
x1677
x1095
x764

asked: Dec 27 '10 at 04:11 AM

Seen: 13544 times

Last Updated: Jan 07 at 08:53 PM