2D Toolkit Forum

2D Toolkit => Support => Topic started by: mradebe_eti on September 18, 2013, 08:31:57 pm

Title: Help with Lives System
Post by: mradebe_eti on September 18, 2013, 08:31:57 pm
Hi,

I need some help with getting my script to work for my lives system. I have already created 4 sprites showing all 3 lives to 0 lives. I need the player to lose a life if they score below the required amount of points (1500pts) or if they run out of time. This is what I have so far...can anyone help?

public class Lives : MonoBehaviour {

   public int startingLives = 3;
   public static int curLives;
   
   // Use this for initialization
   void Start () {
      curLives = startingLives;
   }
   
   void OnLevelWasLoaded(int level) {

         if (level == 08)

            if (Score.score < 1500) {
 
                  curLives--;
         }
         else if (curLives < 0) {
 
                  print ("Game Over!");
               guiText.text = "Game Over!" + curLives.ToString();
               guiText.material.color = Color.black;
            }
   
   }
}

Any help is appreciated.

Thanks :)
Title: Re: Help with Lives System
Post by: gary-unikronsoftware on September 19, 2013, 09:28:13 am
Hi,

You don't say exactly what your problem is you're having, but the first thing I noticed in your script is that you you have your 'game over' code in an else-if block;  so if the player scores less than 1500 points you remove a life from him, but the game over check wouldn't get done as the code has already done the 'if' part.  Just make the curLives check a separate if statement. 

As for the time limit, just make a note of the time when the level starts, the time when the level ends, calculate the total time taken from that and if the time exceeds your chosen limit also remove a life.
Title: Re: Help with Lives System
Post by: mradebe_eti on September 20, 2013, 02:54:15 am
Hi,

My mistake for not being clear. I'm having trouble with getting the sprites representing my lives to function with the script I wrote.

I also made the curLives check a separate if statement, but it still doesn't seem to work. Do I need a part of the script saying to change the sprite when lives decrease?

   public int startingLives = 3;
   public static int curLives;
   
   // Use this for initialization
   void Start () {
      curLives = startingLives;
      guiText.text = "Lives: " + curLives.ToString();
      guiText.material.color = Color.black;
   }
   
   void OnLevelWasLoaded(int level) {

         if (level == 08)

            if (Score.score < 1500) {
 
                  curLives--;
         }
            if (curLives < 0) {
 
                  print ("Game Over!");
               guiText.text = "Game Over!" + curLives.ToString();
               guiText.material.color = Color.black;
            }
   
   }
}

Title: Re: Help with Lives System
Post by: gary-unikronsoftware on September 20, 2013, 09:53:55 am
So are you saying that the "Lives: " and "Game Over" text isn't displaying? Or do you want your character sprite to change when they have no lives left?
Title: Re: Help with Lives System
Post by: mradebe_eti on September 20, 2013, 06:36:24 pm
I'm saying the character sprite for my lives does not change when a life is lost. So it never get's to the point where a player has zero lives and the "Game Over" text can be displayed.
Title: Re: Help with Lives System
Post by: gary-unikronsoftware on September 21, 2013, 04:25:26 pm
It's probaby easier to use the tk2dTextMesh to display the score.  In the hierarchy create a tk2dUITextMesh object then drag it to the livesMesh slot on the script.  Depending on your camera settings you may have to change the scale of the text mesh so it is visible.

Code: [Select]
using UnityEngine;
using System.Collections;

public class Lives : MonoBehaviour
{
public int startingLives = 3;
    public static int curLives;
public tk2dTextMesh livesMesh;

   void Start ()
{
curLives = startingLives;
livesMesh.text = "Lives: " + curLives.ToString();
livesMesh.Commit();
   }
   
void OnLevelWasLoaded(int level)
{
if (level == 08)
{
if (Score.score < 1500)
{
  curLives--;
livesMesh.text = "Lives: " + curLives.ToString();
livesMesh.Commit();
}
            if (curLives < 0)
{
  print ("Game Over!");
                livesMesh.text = "Game Over!" + curLives.ToString();
livesMesh.Commit ();
            }
}
}
}
Title: Re: Help with Lives System
Post by: mradebe_eti on October 08, 2013, 07:34:21 pm
I tried your suggestion to use the 2dUITextMesh and it still is not working.

Here is my script that I attached to the the TextMesh. I'm not sure where I'm going wrong.


Code: [Select]
using UnityEngine;
using System.Collections;

public class Lives : MonoBehaviour {

public int startingLives = 3;
    public static int curLives;
public tk2dTextMesh livesMesh;

void Start ()
{
curLives = startingLives;
livesMesh.text = "Lives    " + curLives.ToString();
livesMesh.Commit();

   }

void OnLevelWasLoaded(int level)
{
if (level == 08)
{
if (Score.score < 1500)
{
  curLives--;
livesMesh.text = "Lives    " + curLives.ToString();
livesMesh.Commit();
}
            if (curLives < 0)
{
  print ("Game Over!");
                livesMesh.text = "Game Over!" + curLives.ToString();
livesMesh.Commit ();
            }
}
}
}

I appreciate the assistance.
Title: Re: Help with Lives System
Post by: unikronsoftware on October 08, 2013, 10:05:38 pm
I'm not sure why you've got an if (level == 8) block around there, surely it'll only work when its at level 8?
Title: Re: Help with Lives System
Post by: mradebe_eti on October 09, 2013, 09:03:36 pm
I forgot to turn off emoticons when posting. It's supposed to be at level 8.
Title: Re: Help with Lives System
Post by: mradebe_eti on October 10, 2013, 10:00:46 pm
I just figured out your comment. I placed an if statement because I wanted it to decrease the player's lives once they reached level 8 and they did not score the required amount of points.