Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

236
Vistas
Unity- Load - Unlock levels and check if there are next levels existing

I am working on small game similar to angry birds since I am new to both unity and C#. I want to make load and unlock next level if all enemies are dead and check if new level exist (if not) return back to Level selection scene.

This is what I tried:

LevelScript

using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelScript : MonoBehaviour
{

[SerializeField] string _nextLevelName;

Monster[] _monsters;




void OnEnable()
{
    _monsters = FindObjectsOfType<Monster>();
}


private void Update()
{
    int currentLevel = SceneManager.GetActiveScene().buildIndex ;
   

    if (currentLevel >= PlayerPrefs.GetInt("levelsUnlocked"))
    {
        PlayerPrefs.SetInt("levelsUnlocked", currentLevel );
    }

    if (MonsterAreAllDead())
    {

        GoToNextLevel();

    }

    Debug.Log("Level" + PlayerPrefs.GetInt("levelsUnlocked") + "UNLOCKED");
}






public void Pass()
{
    int currentLevel = SceneManager.GetActiveScene().buildIndex;

    if (currentLevel >= PlayerPrefs.GetInt("levelsUnlocked") )
    {
        PlayerPrefs.SetInt("levelsUnlocked", currentLevel + 1);
       
    };

}
    
bool MonsterAreAllDead()
{

    foreach (var monster in _monsters)
    {
        if (monster.gameObject.activeSelf)
            return false;

    }

    return true;
}


void GoToNextLevel()
{
    Debug.Log("Go to next level" + _nextLevelName);
    SceneManager.LoadScene(_nextLevelName);


}

}

and Level Manager

 public class LevelManager : MonoBehaviour
{

    int levelsUnlocked;



    public Button[] buttons;

    
    void Start()
    {
        levelsUnlocked = PlayerPrefs.GetInt("levelsUnlocked", 1);
        
        for (int i = 0; i < buttons.Length; i++)
        {
            buttons[i].interactable = false;
        }


        for (int i = 0; i < levelsUnlocked; i++)
        {




            buttons[i].interactable = true;



    }
    }

    




public void LoadLevel(int levelIndex)
    {
        SceneManager.LoadScene(levelIndex);
    
    }

I got these 2 scripts and I attached both canvas and my buttons and Level script to my levels.

Problem is that every level gets unlocked at begin and after completeing levels automatically it want to go to next level whic is not exist yet.

Pls help me. Sorry if my question is stupid and for bad english.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You need to create an array of scenes in your LevelManager that knows all your levels (in order) and to get the next level you can get the position of your actual scene in the array and check the next one.

Something like

pseudocode:

 using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Example() { Application.LoadLevel("HighScore"); } }
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda