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

220
Views
How do I load random levels?

I'm using Unity 3 to build my game. I have a basic GUI button that when clicked, I would like the user to be taken to a random level. There are 10 levels in my game. Below is a copy of the code I'm trying to implement.

function OnGUI()
{
    // Make a background box
    GUI.Box(Rect(10, 10, 100, 90), "Oracle");

    if (GUI.Button(Rect(20, 40, 80, 20), 9)) ;
    {
        Application.LoadLevel(Random.Range(0, 9));
    }
}

It's not happening. I've also tried:

function OnGUI()
{
    // Make a background box
    GUI.Box(Rect(10, 10, 100, 90), "Oracle");


    if (GUI.Button(Rect(20, 40, 80, 20))) ;
    {
        Application.LoadLevel(Random.Range(0, Application.levelCount 9));
    }
}

I've never used the Random.Range function before and somewhat confused at the proper format.

Also I have EZ GUI available and was wondering if I could enter the correct Random Range script into the 'script' dropdown or 'script with method' drop down work it, as I'd rather use a custom button. Any assistance would be greatly appreciated.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Here is the code that worked. I will be rebuilding and adding a few more levels I will try the straight from the Unity docs and see if the random scene loads from just the number of scenes in the build. Thanks DigitalD

static var levelCount : int;

function OnGUI () {
   // Make a background box
    GUI.Box (Rect (10,10,100,90), "ORACLE");

    if (GUI.Button (Rect (20,40,80,20), "Genie")) {
        Application.LoadLevel (Random.Range(0, Application.levelCount-5));
    }
}
over 4 years ago · Santiago Trujillo Report

0

The example code from the Unity docs should work fine. "Application.LoadLevel(Random.Range(0, Application.levelCount))" will load a random scene. The level numbers are based on the order in your build settings. Is is possible you forgot to include them in the build? Just having 10 scenes isn't enough, they'll be stripped out if they're not included in the build.

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!