Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Jointuletz · May 11, 2020 at 11:04 AM · lightingdestroyrandom.rangelevels

Random levels(scenes) without repeat them till last one is played

Hello, I`m a newbie on coding and i don`t know how to do when i start the game to random a level and when finish it to load next random one till all levels(scenes) are played ! i have random,range already

     public void CompleteLevelC()
     {
         int index1 = Random.Range(2, 11);
         SceneManager.LoadScene(index1);
     }


This code randomly play scenes and repeat them continuously. What i want is random them till all scene from 2 to 11 finish without repeat them and then i will load something else. i try with RemoveAt but did not work ...maybe i did it wrong. Any help will be appreciated.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Captain_Pineapple · May 11, 2020 at 11:48 AM

Hey there,

when you use Random.Range(...) it will give you a number between the first number and the second (so 2 and 11) in your case.

So what you need is a List of all your levels and you randomly choose an entry, load that entry and then delete it.

So have a static List<int> LevelsToPlay = new List<int> {2,3,4,5,6,7,8,9,10,11}; as a variable in your class. The static modifier will ensure that the list is not reinstantiated every time you change a scene. If your current object is tagged as DontdestroyOnLoad you do not need this.

and change your code to:

         public void CompleteLevelC()
         {
             //choose the index of a level:
             int nextLevelIndex = Random.Range(0, LevelsToPlay.Count);
             //get the actual sceneIndex by the index of our list:
             int nextLevel = LevelsToPlay[nextLevelIndex];
             //remove the sceneIndex from the list to make it not appear again:
             LevelsToPlay.Remove(nextLevel);
             // load the level:
             SceneManager.LoadScene(nextLevel);
         }

Let me know if anything was unclear.

Comment
Add comment · Show 4 · 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 Jointuletz · May 11, 2020 at 02:15 PM 0
Share

I`m bothering you again :) sorry

It works perfectly and thanks for the help...but now i have another question :)

How can i do after all levels(scenes) are played to send him to other stage for example to the main menu ...

mainmenu is level0 in build settings....

avatar image Captain_Pineapple Jointuletz · May 12, 2020 at 08:45 AM 0
Share

well if you completed all levels the list of levels should be empty correct? So you can just check for that and if the list is empty load your main menu.

avatar image Jointuletz Captain_Pineapple · May 12, 2020 at 05:43 PM 0
Share

Thanks again... i manage to do it :) just attache at the end of the CompleteLevelC function this lines

     if(LevelsToPlay.Count < 0)
       Scene$$anonymous$$anager.LoadScene(leveltoLoad);
avatar image Plougz · Nov 04, 2020 at 11:18 AM 0
Share

If i want to add the previous scene again after deleting it. So it'll be possible to go from map1 > map2 > map1 again, where and how would i do that?

avatar image
0

Answer by Jointuletz · May 11, 2020 at 12:01 PM

Thanks for the answer, i don`t have current object as DontdestroyOnLoad. I need to put it like that? What will be the fast and secure way to do it and how ?:) Thanks.... just add DontdestroyOnLoad (); like this ?

Comment
Add comment · Show 1 · 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 Captain_Pineapple · May 11, 2020 at 12:20 PM 0
Share

if you add the Line DontDestroyOnLoad(gameObject); to a script then the gameObject will not be destroyed when changing scenes. If you are not used to using this then i'd suggest not to use it here since you have to take care of some other things like: Assume you have object A as dontdestroyonload and A is part of Scene 1. Now you change to Scene 2 and back to Scene 1. Now you will have 2 isntances of Object A which will both not be destroyed when you change scenes. So in this case using a static List will be the easier solution for you.

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

175 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

problem Insatiate and destroy auto 0 Answers

Unlockable Levels - Object Masking 0 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

The right way to generate levels like Knife Hit or Paint hit game style. 1 Answer

Light culling using OnPreRender and OnPostRender after updating to 4.3 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