I have a script in C# for player lives, when collision with bullet is detected, player loses 1 live. I would like to add a collision with object tagged "medKit", and then player would get 100 or whatever more lives.
using UnityEngine;
using System.Collections;
public class NewPlayerLives : MonoBehaviour
{
//Fields
public int lives = 50;
public Texture2D h4;
public Texture2D h3;
public Texture2D h2;
public Texture2D h1;
public Texture2D h0;
void OnGUI()
{
if(lives > 40){
GUI.Label (new Rect (900,25,100,100),h4);
}
else if(lives > 30){
GUI.Label (new Rect (900,25,100,100),h3);
}
else if(lives > 20){
GUI.Label (new Rect (900,25,100,100),h2);
}
else if(lives > 10){
GUI.Label (new Rect (900,25,100,100),h1);
}
else if(lives > 0){
GUI.Label (new Rect (900,25,100,100),h0);
}
}
//Trigger Function
void OnTriggerEnter(Collider col){
if (col.tag == "bullet")
{
Debug.Log("Collision with bullet detected");
//Decrease Life and destroy bullet
lives -= 1;
Destroy(col.gameObject);
}
else if(col.tag=="medKit")
{
lives +=100;
}
//Check if we destroy our enemy
if (DidEnemyDie())
Application.LoadLevel(2);
//Destroy(gameObject);
}
//Function return true if life < 0
bool DidEnemyDie()
{
return lives <= 0 ? true : false;
}
}
Unfortunately it's not working, probably this is not the right way to do it. Maybe any of you can tell me what am I doing wrong...
Thanks.