x


variable help

heres my script

var bullet : Rigidbody;
var health = 3;
var enemy : GameObject;

function OnCollisionEnter(bullet : Collision){
var health =- 1;
}

function Update(){
if (health == 0);
Destroy (enemy);
}

i didnt know what to put for the variable near the end to destroy the enemy when its out of health so i just tried random stuff and found that this combination came up with no errors, except the cube is destroyed at the start of the game, i tried

Destroy (enemy,3);

and that just destroyed the enemy 3 seconds after the start of the game

help plz.

more ▼

asked Sep 20 '11 at 07:45 AM

sam32x gravatar image

sam32x
178 38 55 62

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

3 answers: sort oldest

Hi sam11x, sorry, I'm not sure to have understand your question very well...Do you say that the enemy is destroied at the beginning of the execution? In that case, I think that

if (health == 0);

is wrong. To perform the "Destroy (enemy);" instruction inside the "if" statement you have to delete the ";" at the end of that line. So:

function Update(){
if (health == 0)
Destroy (enemy);
}

Let me know.

more ▼

answered Sep 20 '11 at 08:15 AM

BiG gravatar image

BiG
4.7k 4 13 49

woohoo thanks

Sep 20 '11 at 08:57 AM sam32x

Of nothing, sam11x! But thanks @timsk as well...He remarked an important matter, that's the control about the possible-negative health. As he said, in the future, you could create more powerful weapons, that will inflict more than a damage. If the enemy's health will go under 0, he won't die with your actual script.

Sep 20 '11 at 09:14 AM BiG
(comments are locked)
10|3000 characters needed characters left

The correct usage for an if statement is:

if(health ==0)
{
destroy (enemy);
}

such a simple mistake :).

Also, i would change "health == 0" to "health <=0". This will solve a future bug that occurs when the enemies health is below 0, which would become a problem if, for example, there was a different bullet that inflicted 5 damage ;).

more ▼

answered Sep 20 '11 at 08:14 AM

timsk gravatar image

timsk
255 3 6 9

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

remove semicolon at the end of - if (health == 0);

you should have: function Update(){ if (health == 0) Destroy (enemy); }

more ▼

answered Sep 20 '11 at 08:45 AM

Arkhivrag gravatar image

Arkhivrag
16 4 5 7

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

x57

asked: Sep 20 '11 at 07:45 AM

Seen: 288 times

Last Updated: Sep 20 '11 at 09:14 AM