x


shooting crashes game

i am no sure why but whilst playing my game after shooting a certain amount of objects the game crashes any help will be appreciated.

here is my shoot code.

alt text

screenshot1.png (207.5 kB)
more ▼

asked May 13 '12 at 08:20 PM

ryand444 gravatar image

ryand444
18 2 8 11

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

2 answers: sort voted first

Unity can only have so much information at the same time, like everything with computer. You should destroy those bullets after some time, or even better, recycle them. Instantiate only around 20 and shoot them one after the other. Once you shot the 20th, you start again with the first. Instantiate can be expensive.

more ▼

answered May 13 '12 at 08:41 PM

Berenger gravatar image

Berenger
11k 12 19 53

how would i go about destroying them? obviously a set time limit till destruction.

May 14 '12 at 02:26 PM ryand444

Have a look at this guys forum thread about an Object pool for an idea of how to go about recycling them instead of destroying them like @Berenger said.

May 14 '12 at 02:52 PM Lo0NuhtiK
(comments are locked)
10|3000 characters needed characters left

To destroy the bullet after a certain amount of time, the easiest way to do that would be to have a variable in your bullet script, call it "lifetime" or something easy like that. Then make a new function called Awake() and in there, just put Destroy(gameObject,lifetime); .

It'll look like this:

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

*Don't forget this does not go in the update function because it is a whole new function itself.

more ▼

answered May 14 '12 at 02:31 PM

timo0060 gravatar image

timo0060
129 11 25 28

I forgot to show it, but declare a new variable and call it lifetime or something and set it equal to how long you want each bullet to last. Example: var LifeTime: int = 2;

May 14 '12 at 02:33 PM timo0060

thank you very much your help is appreciated.

May 14 '12 at 02:36 PM ryand444

is there anyway to recycle it as it now crashes the game after the set amount of time due to it is trying to accesses the script but the objects have been destroyed.

May 14 '12 at 03:35 PM ryand444

I'm not sure how to do that unfortunately. I destroy my bullet prefabs and my game never seems to crash. Do you have a bullet script and a player script or is it all on one script?

May 14 '12 at 05:45 PM timo0060

its a separate script

May 24 '12 at 01:14 PM ryand444
(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:

x275

asked: May 13 '12 at 08:20 PM

Seen: 341 times

Last Updated: May 24 '12 at 01:28 PM