x


destroying a prefab bullet after time

I am making a game of Battleship, and I came across a problem, I created a script so that the ship can fire projectiles, but it happens that whenever a projectile shooting, I create a clone of the prefab I'm shooting.

So I created a script so that after some time he was self distruted. but I'm having a little problem: he destroy the point where the projectile firing, not the "bullet"! anyone have any idea how I can only destroy the prefab I'm shooting? this is my scpript:

var lifetime = 1.0;

function Awake () {

Destroy(gameObject.Find("bufabola(Clone)").lifetime);

}

more ▼

asked May 10 '12 at 01:51 PM

ruifernandes01 gravatar image

ruifernandes01
23 5 9 14

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

3 answers: sort voted first

Hi,

Firstly I would make a small script that is to be added to the original prefab. Then you can call "this" like so:

var lifetime = 1.0;

    function Awake()
    {
        Destroy(this, lifetime);
    }

This should work so long as the script is attached to the prefab.

more ▼

answered May 10 '12 at 05:45 PM

David C gravatar image

David C
217 26 29 33

One caveat: Destroy(this) will destroy the calling component only. You can call Destroy(gameObject) to destroy the entire object (including children).

May 10 '12 at 06:15 PM rutter

Oh yes, Forgot about that!

May 10 '12 at 06:22 PM David C

well, now it destructs the entire script, and I just want it to destroy the clone of the bullet i'm making when i'm shooting...

this is my cenario, i have a bullet prefab called "bufabola", and i have put it in a sphere that is my point where the bullets are spwan, so i made a script to shot my bullets, and so far no problem, but now when i shoot unity creates a clone of the prefab that i'm shooting, so my hierarchy gains a lot of clones that can slow my game...

Do not now what to do.. any sugestion?

May 11 '12 at 02:09 PM ruifernandes01

No, no, you put that script on the bullet!

May 11 '12 at 02:11 PM syclamoth

Making clones of the prefab is how Unity works, if its a fast moving bullet you can do away with the prefab and use raycasting and particles to fake a bullet object

May 11 '12 at 03:02 PM David C
(comments are locked)
10|3000 characters needed characters left

instantiating and destroying objects is very heavy, you should try to SetRecusivelyFalse your object that you want to delete, and then push them in a array to reuse them later.

more ▼

answered May 10 '12 at 06:42 PM

Stonemove420 gravatar image

Stonemove420
22 2 4 8

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

tanks guyz! it is working! :D next stept create an explosion when colliding with the other boats and then sink slowly. wish me luck!

and again tanks a lot! :D

more ▼

answered May 14 '12 at 03:12 PM

ruifernandes01 gravatar image

ruifernandes01
23 5 9 14

(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:

x1288
x785
x320

asked: May 10 '12 at 01:51 PM

Seen: 1499 times

Last Updated: May 14 '12 at 03:12 PM