- Home /
Level Unlock system
Hi all
kindly i need your support in my Unlock level system , i finlized the following steps :
first i built the UI as the following screen shoot :
then i use the LevelSelector script and its working fine as the followin screen shoot :
the LevelSelector script syntax code as the following :
using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class LevelSelector : MonoBehaviour {
//public SceneFader fader;
public Button[] levelButtons;
void Start ()
{
int levelReached = PlayerPrefs.GetInt("levelReached", 1);
for (int i = 0; i < levelButtons.Length; i++)
{
if (i + 1 > levelReached)
levelButtons[i].interactable = false;
}
}
public void changeToScene(int changeTheScene)
{
SceneManager.LoadScene(changeTheScene);
}
}
then i used the enemy health manager as the following :
using UnityEngine; using System.Collections;
public class EnemyHealth :MonoBehaviour {
public float curHealth;
public float maxHealth;
void Start () {
curHealth=100f;
maxHealth =100f;
}
void Update () {
EHealthManager (0);
}
public float EHealthManager(float f){
if (curHealth > maxHealth)
curHealth = maxHealth;
if (curHealth < 0)
curHealth = 0 ;
if (maxHealth < 1)
maxHealth = 1;
curHealth += f;
float modifiedHealth = (curHealth / maxHealth) * 500;
return modifiedHealth;
}
void OnGUI(){
GUI.Box(new Rect(20,50,EHealthManager(0),25), ((int) curHealth).ToString() + "/" + ((int)maxHealth).ToString());
}
}
then i use LevelManager script to unlock the next level "button2" as the following :
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class LevelManager : MonoBehaviour { public string nextLevel = "Level-2"; public int levelToUnlock = 2; public GameObject EnemyHealth;
public void LoadNextLevel()
{
if(EnemyHealth.GetComponent<EnemyHealth>().curHealth /EnemyHealth.GetComponent<EnemyHealth>().maxHealth <= 0){
int currentUnlock = PlayerPrefs.GetInt("levelReached");
if (currentUnlock < levelToUnlock) {
PlayerPrefs.SetInt("levelReached", levelToUnlock);
}
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
PlayerPrefs.SetInt("SavedLevel1", 1);
Debug.Log("SavedLeve1 = 1");
}
}
}
my issues :
I need ( level manager script) doing the following : 1. If the player health less than zero restart the game that mean he lose to win 2. If the total enemy’s health less than zero , message you win then load next level 3. If the total enemy’s health less than zero unlock button level 2 4. Save all level status 5. how to restart the unlock system
appricated your kindy support to fix level manager script where i dont face any error but its not working ,