if statement >?

ey i want to make when my “lives” reaches 0 that i will go game-over and that the game-over screen-scene opens up
but i dont know how to can someone help em this is what i got:

if ( HealthControl.LIVES ) {
// pick up object
HealthControl.LIVES = 0;
Application.LoadLevel (0);
}

but now i cant lose lives anymore i will show you the whole script if is needed…

that code looks totally wrong.
setting lives to 0 if you are already on 0 makes no sense and setting it to 0 when its not 0 only makes sense if you can at max have 1 life.
I think neither is really what you want to have

please can you or someone else help me this is the whole script with other crap
but what i want is above ^^^
this is the script so far -the piece above:

private var dead = false;
private var fallout = false;
private var teleport = false;
private var teleport2 = false;
private var heart = false;

static var maxLIVES = 5;

function OnControllerColliderHit(hit : ControllerColliderHit)
{
if(hit.gameObject.tag== “fallout”)
{
fallout = true;
//substract life here
HealthControl.LIVES -= 1;
}
if(hit.gameObject.tag== “enemyProjectile”)
{
gotHit = true;
//substract life here
HealthControl.LIVES -= 1;
Destroy (hit.collider.gameObject);
}

if ( HealthControl.LIVES < maxLIVES ) {
// pick up object
if(hit.gameObject.tag== “heart”)
{
gotHit = true;
//Add Lives here !! YEAH !
HealthControl.LIVES += 1;
Destroy (hit.collider.gameObject);
}
} else
// don’t pick up object
{
gotHit = true;
HealthControl.LIVES += 0;
}
}

function Update ()
{
}

function LateUpdate()
{
if(fallout)
{
transform.position = Vector3(-24.6,7,0.2);
gameObject.Find(“Main Camera”).transform.position = Vector3(-24.6,8,-14.8);
fallout = false;
}
if(dead)
{
transform.position = Vector3(0,1,0);
gameObject.Find(“Main Camera”).transform.position = Vector3(0,2,-15);
dead = false;
}
}

@script RequireComponent(CharacterController)

if ( HealthControl.LIVES==0 ) {
Application.LoadLevel (0);
}

just to add … the EyeSix code is correct… don´t forget to place it in Update()

No idea what this script is meant for or what it’s supposed to do, but you can also move the stuff out of LateUpdate and just change it when the actual state change occurs. For example, you’re setting “fallout = true;” in the collision code, so you might as well move the camera in that part instead of checking for it every single frame. You won’t need the “fallout” variable this way, nor the “dead” variable (which is never being set to True, by the way).

Also, “HealthControl.LIVES += 0;” should just be removed. Hopefully I don’t have to explain why.

Okay, your math is a little wonky here and there. You suffer from a condition I call “Ifitis”. It’s the clinical term for anyone who uses if-statements when some simple mathematics would solve the problem for you.

You should never have to check to see if you can add a value to something before it reaches the maximum cap, and you should never have to check to see if you can subtract before it reaches the minimum.

if(lives<=MAXLIVES) //bad kitty!
{
  lives += 1;
}

Isn’t this so much nicer?

lives = Mathf.Min(lives+1,MAXLIVES);

How about we make this bi-directional? (works with adding and subtracting)

float inc = -1 //or +1

lives = Mathf.Min(Mathf.Max(0,lives+inc),MAXLIVES); //you can also use a nifty clamp function with the same effect

This stuff is USEFUL!

To which the answer is no! The intent in the second example is not as clear (handy if you ever plan on sharing your code) and more importantly it’s significantly slower. If you are worried about the code length you can easily collapse it into one line.

Maybe you suffer from “functionitis”? :stuck_out_tongue:

Why would the second example be significantly slower than the first? Have you actually measured it?

(Not saying you’re wrong - I’d just be interested in knowing how you made this determination.)

TerXIII, your code’s readable, but Ilike JohnnyA I prefer the if-syntax and parse it quicker.

if ( lives < MAXLIVES ) { ++lives; }

My money is on the if syntax being faster because (a) it only requires an incr in some cases rather than all cases and (b) it doesn’t require the overhead of calling out to and returning from a function. But with the intelligence of modern compilers–which auto-optimize code like (a+1) into (a++) to make them run more efficiently, and sometimes run functions inline, it’s hard to tell without an benchmark.

I don’t believe the performance of this statement is particularly important to the application at hand.

Agreed, and:

Agreed.

Okay, I ran the benchmark out of morbid curiosity. :slight_smile:

In the one corrner, we have the if approach:

		for (long i = 0; i < 2000000000; ++i) { // 2,000,000,000
			if (lives < maxLives) lives++;
		}

In the other corner, we have the Min() approach:

		for (long i = 0; i < 2000000000; ++i) { // 2,000,000,000
			Mathf.Min(lives+1, maxLives);
		}

The code in common was:

		const long maxLives = 500000000; // 500,000,000
		long lives = 0;

… and the timings are…

The if approach:
11 seconds, 11 seconds, 11 seconds

The min() approach:
31 seconds, 31 seconds, 31 seconds

… and the winner is …

.
.
.

The if statement!

Although we totally agree performance is not particularly important in this case so other factors like readability are more important and when performance matters real benchmarks are crucial. :slight_smile:

(I was just curious since we were having the discussion…)

It’s pretty obvious that it is going to be slower given that an if followed by an optional increment is pretty close to a perfect implementation (assuming the script compiler isn’t completely stupid). The min function is almost certainly made up of an if statement… something like:

function Min(a:float, b:float) {
if (a < b) return a;
return b;
}

On top of this the second method always increments and always performs an assignment (a typically slow operation).

But despite that I did measure it. Empty scene with an update method (in JS) calling the first method 100k times each Update runs at approx. 800 fps on my system. With the second function substituted it runs at about 200fps. Obviously this is not definitive, but combined with a basic analysis of the code it was conclusive enough for me. Sorry I should have included these notes in my original post.

Games are typically an area where optimisations will matter; particularly if you’re deploying to mobile platforms (which many Unity users are). In this case its probably not an issue; however making blanket statements with an overbearing tone, then giving such a terrible example, seems to be bad form.

EDIT: Just fixing some spelling mistakes :slight_smile: