Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

234
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!