Collison Kills

Im wanting to make it so that when my ball collides with an object it is killed and looses a life at three lifes gone it looses game i know how to run game over just having problems with the lifes. I was wondering if some one could give me a basic code to get stated with im a little new to trying to design for android devices.

1 Answer

1

You must create a control script - let’s call it Control.js - to hold all important variables (like health, lives, points etc.) and attach it to an empty object (or any object that’s not destroyed during the level) - this could be the Control script:

static var lives = 3;  // control the player lives

function Update(){
    if (lives <= 0){
        // Game Over
    }
}

And in the ball script:

function OnDestroy(){
    Control.lives -= 1; // decrement one life when the ball is destroyed
}

Ok, you're not destroying the ball, just moving it back to the spawn point. In this case, you can decrement the lives in your trigger event: <pre> function OnTriggerEnter(other: Collider){ Control.lives -= 1; // decrement life count other.gameObject.active = false; ... </pre> You can also attach the Control script to the ball, since it's not being destroyed.