- Home /
enemy explode
i want my enemy to destroy and explode but it wont
var life = 0;
var explosion : GameObject;
function OnCollisionEnter(boom : Collision) {
if (explosion.active == false)
if(boom.gameObject.tag == "bullet")
{
life +=1;
if(life == 30)
Destroy(gameObject);
explosion.SetActiveRecursively(true);
}
}
You forgot the braces '{' and '}' before line 10 and after 11 respectively.
I reformatted your code. I don't see any syntax problems with braces.
@flaviusxvii: He wants to destroy the gameobject (line 10) and activate the explosion (line 11) but his "if" doesn't have braces so it only recognizes the line immediately after the if (which is line 10)...
Ahh.. this is why I ALWAYS USE BRACES AROUND BLOC$$anonymous$$S. Nice catch.
@Sajidfarooq the first bullet will activate the explosion (assu$$anonymous$$g it is inactive to begin with), and then no further collisions will enter if(boom...
Answer by Sajidfarooq · Sep 05, 2013 at 09:44 PM
You forgot the braces '{' and '}' before line 10 and after 11 respectively, i.e, after the if(life==30) block. Your code should look like this:
var life = 0;
var explosion : GameObject;
function OnCollisionEnter(boom : Collision)
{
if (explosion.active == false)
if(boom.gameObject.tag == "bullet")
{
life +=1;
if(life == 30)
{
Destroy(gameObject);
explosion.SetActiveRecursively(true);
}
}
}
The problem, as-is, is that the first bullet hit will "always" cause an explosion, regardless of whether life = 30 or not.
Answer by William · Sep 05, 2013 at 09:49 PM
Ok so all i need is brackets on if
{if}(life == 30)
No, see my answer. Also, please don't use the "answer" tool to add a comment, like you did. Answers are only for...well...answers, not clarifications.
Furthermore, when you like an answer, remember to mark it as "answered" so that others can benefit by knowing what the correct solutions is.
Your answer
![](https://koobas.hobune.stream/wayback/20220613113641im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Music Wont Stop From Page Script+ Extra Problom 1 Answer
Instantiation Wont Stop 1 Answer
c# enum wont show in inspector 3 Answers
Unable to Stop a Coroutine in C# 1 Answer
How do I stop coroutine? 2 Answers