I’ve read several threads and discussions similar to this subject, and tried many methods; but still seem to be doing something wrong…
I have a script that controls the health, a player script, and a script to load a level when I come to the game over screen.
I basically wanted to reset the player score back to 0 and the LIVES back to 3 after I die, or win so that it won’t keep sending me back to the gameOver/Win screen.
Here is the health script:
var health1 : Texture2D; //onelife
var health2 : Texture2D; //twolives
var health3 : Texture2D; //fullhealth
static var LIVES = 3;
function Update ()
{
switch(LIVES)
{
case 3:
guiTexture.texture = health3;
break;
case 2:
guiTexture.texture = health2;
break;
case 1:
guiTexture.texture = health1;
break;
case 0:
//gameover script "here"
Application.LoadLevel(2);
break;
}
}
Here is my Player Script:
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
var movement : PlatformerControllerMovement;
static var dead = false;
static var gotHit = false;
// The character will spawn at spawnPoint's position when needed. This could be changed via a script at runtime to implement, e.g. waypoints/savepoints.
var spawnPoint : Transform;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function FixedUpdate() {
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags CollisionFlags.CollidedBelow) != 0;
}
static var playerScore : int;
//GUI Score
function OnGUI(){
GUI.Label(Rect(10,10,100,50), ""+ playerScore);
}
function Update() {
if(playerScore >= 6) {
Application.LoadLevel (3);
playerScore = 0;
LIVES = 3;
}
}
function Awake () {
movement.direction = transform.TransformDirection (Vector3.forward);
controller = GetComponent (CharacterController);
Spawn ();
}
function Spawn () {
// reset the character's speed
movement.verticalSpeed = 0.0;
movement.speed = 0.0;
// reset the character's position to the spawnPoint
transform.position = spawnPoint.position;
}
function OnDeath () {
Spawn ();
}
function OnTriggerEnter( hit : Collider){
if(hit.gameObject.tag == "EnemyBullet")
{
dead = true;
gotHit = true;
HealthControl.LIVES -= 1;
}
if(hit.gameObject.tag == "Enemy")
{
dead = true;
gotHit = true;
HealthControl.LIVES -=1;
}
}
@script RequireComponent(CharacterController)
And heres the game over script:
var isPlayAgain = false;
var isQuitButton = false;
function OnMouseEnter()
{
//change the color of the text
renderer.material.color = Color.green;
}
function OnMouseExit()
{
//change the color of the text
renderer.material.color = Color.white;
}
function OnMouseUp()
{
//are we dealing with a Quit Button?
if( isQuitButton )
{
//quit the game
Application.Quit();
}
if( isPlayAgain )
{
//play again
Application.LoadLevel(1);
}
}
I’ve been told to change the static variables to private; but I usually get errors; considering I have other scripts that relate to those variables.
I’ve also tried putting playerScore = 0; and LIVES = 3; underneath, and above the application.LoadLevel scripting.
Does anyone have suggestions on what to place and where to place it? =)