- Home /
How can I add an extra life?
I am currently trying to make a collectible when all coins have passed then you would get an extra life there. Maybe someone knows how I should go fix?
below I have a small example was added.
//// example /////
void OnGUI() {
string[] split = Application.loadedLevelName.Split("_"[0]);
if(levelwon)
{
Logic logic = GameObject.Find("Logic").GetComponent("Logic") as Logic;
if(logic.GetBasherScore(9)== true)
{
GUI.DrawTexture(new Rect(Screen.width/2-280,20,80,80), trophy);
}
for(int i=1; i<7; i++)
{
if(logic.GetBasherScore(i)== true)
{
GUI.DrawTexture(new Rect(bbposx[i-1],bbposy[i-1],40,40), bbmunten[i-1]);
}
}
Answer by Vonni · Jan 04, 2013 at 10:52 PM
you could have a script on each collectible that prompt a function on your main script.
// collectibleScript.cs
function OnCollisionEnter(collision : Collision){
if(collision.tag.equals("Player"){
mainScript.addCollectible();
Destroy(gameObject);
}
}
// mainScript.cs
var totalCollectibles : int = 20;
var currentCollected : int = 0;
var life : int = 3;
function addCollectible(){
currentCollected++;
if(currentCollected == totalCollectibles)
life++;
}
EDIT: Woops, forgot to write in C#, but should be easy to translate
Hello.
Thank you that code helped me out as well. I code in C# and am able to get this working. Thank you.
Your answer
![](https://koobas.hobune.stream/wayback/20220613090003im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
player life lose when timer is run out 1 Answer
Decrease life from another script 1 Answer