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 KnightRiderGuy · Feb 06, 2015 at 05:32 PM · arraysloadingloadlevelrandom.range

What is a simple way to load a random scene from an array? C#

I'm trying to figure out how to load a random scene from an array of scenes using C#

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Glurth · Feb 06, 2015 at 05:42 PM

This function takes a STRING as a parameter, http://docs.unity3d.com/ScriptReference/Application.LoadLevel.html

It also takes an index, but not sure how to use that (though it would probably eliminate the need for the string array described below.)

So, all you need to do is create an array of say.. 10 strings, with the names of your scenes, as displayed in the editor. (this is un-compiled code, may have some errors, but you'll get the idea)

 string[] sceneArray= {"openeing", "scene1", "closing".... };

Then you can use Random.Range, to select one

 string sceneToRun= sceneArray[Random.Range(0,sceneArray.Length-1)];

Then launch it. Application.LoadLevel(sceneToRun);

Comment
Add comment · Show 8 · 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 KnightRiderGuy · Feb 06, 2015 at 05:54 PM 0
Share

I tried this but I get errors? //Scene Array string[] sceneArray= {"RadarScreen", "RadarScreen02", ""};

     string sceneToRun= sceneArray[Random.Range(0,sceneArray.Length-1)];
     Application.LoadLevel(sceneToRun);
avatar image Glurth · Feb 06, 2015 at 06:04 PM 0
Share

I didn't test it out myself, what error do you get? Do you have a scene named "" ? Probabaly not, so I'd change that to a valid name, or remove it. also, you can test by putting the name DIRECTLY in your LoadLevel call, an ignore the array stuff, till you get this part working.

avatar image KnightRiderGuy · Feb 06, 2015 at 06:29 PM 0
Share

2 errors. I did put actual names of the scene in the array though? Assets/Scripts/SystGuidanceRadarButton.cs(12,30): error CS1519: Unexpected symbol (' in class, struct, or interface member declaration and Assets/Scripts/SystGuidanceRadarButton.cs(12,41): error CS1519: Unexpected symbol )' in class, struct, or interface member declaration

avatar image Glurth · Feb 06, 2015 at 08:51 PM 0
Share

oh, compiler errors? I assumed you would be able to handle those. I see below you have a "code guy".. he should be able to fix those lickety-split: just send him a link to this question.

(Or you can google those errors to try and resolve them yourself. I'm sorry bud, just not into $$anonymous$$ching c#, at least not on this site.)

avatar image Owen-Reynolds · Feb 07, 2015 at 05:25 AM 0
Share

Glurth: using scene numbers is easy. Same idea, but: int[] Snums={3,7,9,12}; then LoadLevel(Snums[i]);.

Show more comments

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

20 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

Related Questions

Multiplayer Level Loading 1 Answer

How to load an inventory array? 1 Answer

How to make a loading screen? 2 Answers

Loadingscreen and play Level when initialized completely 1 Answer

Why does Application.LoadLevelAsync cause a huge spike? 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