Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mmangual_83 · Dec 17, 2013 at 01:48 PM · c#randomloadlevel

Load levels randomly

Greetings! I am working on this game for school and I want to know how to get this feature started: I need to create a class (or classes, if need be) that loads levels randomly every time and once all levels have been loaded, we stop loading and close the application.

Here is my code snippet:

 using UnityEngine;
 using System.Collections;
 
 [ExecuteInEditMode]
 public class SceneManager : MonoBehaviour {
     public enum LoadMyScene
     {
         scene1, scene2, scene3 
     }
     [HideInInspector]
     public static int nextscene = Random.Range(0, 2); //need this when the user clicks on the "next" button  
 
     public LoadMyScene currentScene = (LoadMyScene)nextscene;
 
     private int counter; //increases every time a scene is loaded
     protected const int MAX = 3;
 
     void Start()
     {
         counter = 0;
     }
     void Update()
     {
         if (counter > MAX) { Application.Quit(); }
         
         switch (currentScene)
         {
             case LoadMyScene.scene1:
                 Application.LoadLevel(1);
                 break;
             case LoadMyScene.scene2:
                 Application.LoadLevel(2);
                 break;
             case LoadMyScene.scene3:
                 Application.LoadLevel(3);
                 break;
         }
     }
 }


As you can see there are several key elements missing and I want to know what am I missing to get this class to a working state.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Guts · Dec 17, 2013 at 04:07 PM 0
Share

Consider an approach where you have an array of integers, and each entry in the array is a valid level number (something that could be passed as an argument to Application.LoadLevel()). If you then randomly shuffle that array, you now have a random ordering of the levels. This Wikipedia page might give you some ideas on how to randomly shuffle an array.

avatar image mmangual_83 · Dec 17, 2013 at 04:17 PM 0
Share

@Guts: thank you for the reply, the explanation is pretty good to understand, however, could you show me an example in code of how to start this? I'm a little new to this. Thank you!

avatar image TinkerinThinker · Dec 17, 2013 at 04:32 PM 0
Share

You want to repeatedly load a random level (each level loading immediately after the previous one has finished loading such that almost no time is spent in each level) until every level has been loaded. Am I understanding this correctly?

avatar image mmangual_83 · Dec 17, 2013 at 05:04 PM 0
Share

@TinkerinThinker: no, each level has a button that,after the user clicks on it, it loads a random scene. But I do not want the same level being loaded over and over so I need a tracker that will keep count of which level is being loaded and stores it so that the next time the user clicks on the load scene the same scene is not loaded again. Once all scenes have been loaded then the application will close.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Briksins · Dec 17, 2013 at 04:27 PM

Hi there in your example it looks you loading level in update method without any additional check / stops or state changes

where is your track of current scene? you need to implement some kind of routine to change your state and also load level only once, but not on each update

also you didn't specify is it allowed to load the same scene repetitively for example if scene 1 was loaded then scene 2 was loaded and Random.Range(0, 2) will give you again 1 which was already loaded what would be in that case?

it should looks like that:

 public class SceneManager : MonoBehaviour {
 
     private ArrayList scenesWereAlreadyLoaded = new ArrayList(); 
  
     void Start()
     {
         int currentSceneToLoad; =  Random.Range(0, 2);
         scenesWereAlreadyLoaded.add(currentSceneToLoad);
         Application.LoadLevel(currentSceneToLoad);
     }
     void Update()
     {
         if(userClickedNextButton)
         {
             while(true)
             {
                 int sceneToLoad = Random.Range(0, 2);
                 if(!scenesWereAlreadyLoaded.Contains(sceneToLoad))
                 {
                     scenesWereAlreadyLoaded.add(sceneToLoad);
                     Application.LoadLevel(sceneToLoad);
                     break;
                 }
             }
         }
         
         if(scenesWereAlreadyLoaded.Length > 2)
             Application.Quit();
         
     }
     
 }



but even this is very messy and you should optimise it for your current task, this is just quick example to demonstrate you the logic

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mmangual_83 · Dec 17, 2013 at 06:27 PM 0
Share

@Briksins Thank you for your reply. What you wrote made a lot of sense but for some reason it doesn't translate well into my design. Could you show me how to fuse what you wrote into $$anonymous$$e to see where everything should go? Thank you.

avatar image mmangual_83 · Dec 17, 2013 at 08:05 PM 0
Share

@Briksins: hi, this is what I managed to get but it still doesn't want to work.

 using UnityEngine;
 using System.Collections;
 
 [ExecuteInEdit$$anonymous$$ode]
 public class Scene$$anonymous$$anager: $$anonymous$$onoBehaviour
 {
     public static bool userClickedNextButton;
 
     protected const int $$anonymous$$AX = 2;
 
     private ArrayList scenesWereAlreadyLoaded = new ArrayList();
 
     void Update()
     {
         if (userClickedNextButton)
         {
             while (true)
             {
                 int sceneToLoad = Random.Range(1, 2);
                 if (!scenesWereAlreadyLoaded.Contains(sceneToLoad))
                 {
                     scenesWereAlreadyLoaded.Add(sceneToLoad);
                     Application.LoadLevel(sceneToLoad);
                     break;
                 }
             }
         }
         if (scenesWereAlreadyLoaded.Count > $$anonymous$$AX) {  Application.Quit(); }
     }
 }
avatar image
0

Answer by Hoskins355 · Apr 24, 2014 at 02:47 PM

 var scenesWereAlreadyLoaded = new ArrayList();
 var currentSceneToLoad : int;
 
 currentSceneToLoad = Random.Range(1, Application.levelCount);
 scenesWereAlreadyLoaded.Add(currentSceneToLoad);
 Application.LoadLevel(currentSceneToLoad);
 
 if(userClickedNextButton){
 while(true){
 currentSceneToLoad = Random.Range(1, Application.levelCount);
          if(!scenesWereAlreadyLoaded.Contains(currentSceneToLoad)){                                       scenesWereAlreadyLoaded.Add(currentSceneToLoad);
 Application.LoadLevel(currentSceneToLoad);
 break;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Follow-up from previous question: "Load levels randomly" 0 Answers

Multiple Cars not working 1 Answer

Random message generator out of control 1 Answer

Making a camera list 1 Answer

Unity random vs C# random 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges