Hi Jan,
I am going explain you that I did, but maybe someone else implemented this in a different or easier way.
I made a prefab Lives
Every each startX is a life. I added that prefab in every level scene. Next, I implemented a class GlobalData using Singleton pattern, the class look like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class GlobalData {
private static GlobalData _instance = null;
private int lives = 0;
private int lastLoadedLevel = 0;
public static GlobalData getGlobalData()
{
if (_instance == null) _instance = new GlobalData();
return _instance;
}
public void setLives(int n)
{
lives = n;
}
public int getLives()
{
return lives;
}
public void setLastLoadedLevel(int level)
{
lastLoadedLevel = level;
}
public int geLastLoadedLevel()
{
return lastLoadedLevel;
}
}
Next in ManagerWizard I implement a function updateLevel, where the program searchs the gameobject “start”+ live for changes its sprite. The program call a function that set the ball on paddle and decrements the lives.
public void updateLevel()
{
GameObject.Find("start" + GlobalData.getGlobalData().getLives()).GetComponent<SpriteRenderer>().sprite = liveLost;
GameObject.FindObjectOfType<Ball>().setInitialPosition();
GlobalData.getGlobalData().setLives(GlobalData.getGlobalData().getLives() - 1);
}
That function is called in LoseCollider, where the user goes to LoseScene only when lives is equal zero and in other case call the function updateLevel
void OnTriggerEnter2D (Collider2D collider) {
if (GlobalData.getGlobalData().getLives() == 0) {
manager.goLoseScene();
}else
{
manager.updateLevel();
}
}
By other hand, in loadNextLevel, goGame (first scene) I set the Lives in 3 after LoadLevel, example:
public void loadNextLevel(){
Brick.brickSize = 0;
GlobalData.getGlobalData().setLastLoadedLevel(Application.loadedLevel + 1);
Application.LoadLevel(Application.loadedLevel + 1);
GlobalData.getGlobalData().setLives(3); //<== here
}
Additionally in the method Update of ManagerWizard is called the updateLevel function when the lives set in 3, the scene is a level scene and the current level is the same level loaded, because of that you see in GlobalData lastLoadedLevel attribute, for control the updateLevel and retry the current level if the user wants. This is done, because i want to decrement the live in use starting the level.
void Update()
{
if(GlobalData.getGlobalData().getLives() == 3 && GameObject.FindObjectOfType<Ball>() != null && Application.loadedLevel > 0 && Application.loadedLevel < 6 && Application.loadedLevel == GlobalData.getGlobalData().geLastLoadedLevel())
{
updateLevel();
Debug.Log("CurrentLevel "+ DateTime.Now + ": "+ GlobalData.getGlobalData().geLastLoadedLevel());
}
}
I hope my explanation is not very tangled and helps you.