Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

239
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda