How do you add life just after completing a level?

I have spent a few days trying to find a way to add an extra life just after completing each level for my block breaker game. I’m stuck ! There are no tutorials on this ? My game has 10 levels and I don’t like the concept of starting with 10 lives with one extra life when you get to 0.

Any help on this subject would be greatly appreciated thanks :smiley:

1 Like

Hi Louise,

do you have any code you can post to show us what you’ve tried so far? I had a similar problem but I tied my life-gain to life per 100 points.

Since you want to tie your life gain to per level completed you’ll probably want to have a variable that keeps track of your total lives and a method to increment your total lives that’s tied to level completion.

The logic should be something like: on level completion increment total lives by 1.

Thanks for your reply. I have not really got any code for it. Anything i tried i removed as did not want it to create any bugs.

My life system is working but not exactly how i want it to. I think many of us have had same problem but there is no tutorial or code references on how to do it. I understand it will be a variable but i am stuck on how to implement this to my game.

Hi Louise,
you said you do have a life system working, even if not how you’d like it to work.
In order for somebody to help you, they would need to see the logic in your existing code. So could you pass the code of your life system here?
I am probably not able to help you, as I am still struggling with my own coding logic, but I would like to try.
cheers
Patricia

1 Like

ok Cool i will thanks

1 Like

‘’'using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class GameplayController : MonoBehaviour
{
public static GameplayController instance;
[SerializeField]
private TextMeshProUGUI lifecount;

private void Awake()
{
    MakeInstance();
}

// Start is called before the first frame update
void Start()
{
    CountLives(LoseColider.numLives);

}

// Update is called once per frame
void MakeInstance()
{
    if (instance == null)
    {
        instance = this;
        print("making instance");
    }
}
public void CountLives(int numLives)
{
    Debug.Log("Number of lives is" + numLives);
    lifecount.text = "Lives: " + numLives;
}

}

‘’’
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LoseColider : MonoBehaviour {

public static int numLives;


private SceneManager sceneManager;

[SerializeField]
private AudioClip ballLostSound;

private void Start()
{
    if (SceneManager.GetActiveScene().name == "Level 1")
    {
        numLives = 3;

    }

}


 private void OnTriggerEnter2D(Collider2D collision) {
    
    numLives--; 
    if (numLives < 0)
    {
        SceneManager.LoadScene("Game Over");
        
        
    }
    else
    {
         GameplayController.instance.CountLives(numLives);
        Ball.instance.SetBallPosition();
    }
    AudioSource.PlayClipAtPoint(ballLostSound, transform.position, 1f);
}

}
‘’’

My lives system is on my GameplayController/LoseColider script

Currently my life system works on the principle on numLives = 3; ( LoseColider Script ) which display at top left of the screen using Text Mesh Pro. Everytime ball hit lose collider you loose life. I spelt Lose Colider with one l but never mind i just corrected it. However, you actually get 4 lives because you get an extra life at 0 which isn’t so good but not sure how to change that.

I want my lives system to add 1 life when player completes a level. My display at start of game is Lives: 3 i don’t want player getting an extra life at Lives: 0

try
if(numLives <= 0)
instead to fix the extra life at 0. It should work.
Basically with (numLives < 0 ) you ask the code to wait for the number of lives to be smaller than 0, so when it hit zero it will wait for -1. (Don’t know if I’m making sens haha)
If you tell your code (numLives <= 0) it’s saying “when the numLives is zero or smaller”

For adding a life every level, can you show your code where there is the win condition?
What I did was that I referenced the numLives from another script to where the win condition is, then added to it.

Awe thanks Carre your a star ! yeh that makes sense. I will find the script where the win condition is

‘’'using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Level : MonoBehaviour
{
// parameters
[SerializeField] int breakableBlocks; // Serialized for debugging purposes

// cached reference
SceneLoader sceneLoader;


private void Start()
{
    sceneLoader = FindObjectOfType<SceneLoader>();

}

public void CountBlocks()
{
    breakableBlocks++;
}

public void BlockDestroyed()
{
    breakableBlocks--;
    if (breakableBlocks <= 0)
    {
     
        sceneLoader.LoadNextScene();   
    }
}

}
‘’’

Right, try this

// cached reference
SceneLoader sceneLoader;
LoseColider loseColider;

private void Start()
{
sceneLoader = FindObjectOfType< SceneLoader >();
loseColider = FindObjectOfType< LoseColider >(); //to reference the other script with the numLives variable

}

public void CountBlocks()
{
breakableBlocks++;
}

public void BlockDestroyed()
{
breakableBlocks–;
if (breakableBlocks <= 0)
{
loseColider.numLives++;
sceneLoader.LoadNextScene();
}
}

I don’t have my own code right now so I don’t know exactly if it’s what I did but it should work… in theory haha :smile:
Try it and tell me if it works :slight_smile:

will do ! your a life saver i owe you one here lol

Gosh, I would love to have code in my mind like Carre…
@ Louise, glad you got help:)

2 Likes

Me too Carre is very good especially at code

1 Like

getting the red squiggly line says "cannot be accessed with an instance reference qualify it with a type name instead hmmmm

hmm I’ll check my code tonight

OH! wait ok
try in your cached reference
int numLives;

in start
numLives = loseColider.numLives;

then instead of “loseColider.numLives” where you load the next scene, write
numLives++;

if this doesn’t work I’ll have to check how I did it in my code tonight.

Thank youu guys :smiley: :relaxed:

will do now and again thankyou so much for your help on this

Privacy & Terms