- Home /
object passes certain point in space
In C#, and in an IF statement.
I'm trying to use an IF statement to compare if an object has passed a certain point. This is how it looks so far:
void Update ()
{
//lose life if ball falls off edge
if (transform.position.y < -5.0f)
{
Lives --;
}
}
However, this causes the "lives" to decrease infinitely.
All I want is after "Lives --;" executes, to STOP this IF statement.
Answer by Lo0NuhtiK · Apr 26, 2012 at 07:51 AM
Be better off adding a game object cube below your playing surface at the distance you want it to 'die' at. Set it's renderer off, but keep a box collider on it. Smash it down a bit thinner than it starts out as, then stretch it out beneath your playing area and out wider than it's borders to make sure you hit it. Then add an OnTriggerEnter function script to that cube which checks to see if the player dropped into it or not and then if it was the player, do something to your player life-count etc.
But, if you want to do it the way you show above ; then somethin like this->
int lives = 4 ;
bool dead = false ;
void Update(){
if(!dead)
CheckWhereImAt() ;
}
void CheckWhereImAt(){
if(transform.position.y < -5.0f){
lives -- ;
dead = true ;
}
}
Actually that's what I WANT to do, I actually did something like that in another project but I cannot get it to work here :( ????? I really have no clue why, so I'm trying this method ins$$anonymous$$d. If you'd like to offer code on how to do the 1st method you talked about, it'd be great as maybe it'll help me fix $$anonymous$$e or try something different.