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 TimoVssn · Mar 02, 2014 at 09:21 AM · randomloadlevel

how to load random level but one?

this is my script:

 #pragma strict
 
 var fouts : AudioClip;
 private var hit: RaycastHit;
 
 function Start () {
 }
 
 function Update () {
 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 if (Input.GetMouseButtonDown(0))
 {    
     if (Physics.Raycast (ray, hit , Mathf.Infinity))
     {
         
         if (hit.collider.tag == "goed")
         {
         Application.LoadLevel(Random.Range(2, 11));
         highscore.score += 1;
         }
         if (hit.collider.tag == "fout")
         {
         audio.Play();
         Application.LoadLevel("gameover");
         
         }
     }
     }
 }

now i want it to load a random level from 2 to 10 but number 4 how do you set this up?

Comment
Add comment · Show 3
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 Lo0NuhtiK · Mar 02, 2014 at 09:24 AM 0
Share

What do you mean? A random level EXCLUDING levels 0, 1, and 4 ?

eg : a random number from this-> (2,3,5,6,7,8,9,10) ?

Edit : Well, anyway, here's some Search Bar $$anonymous$$agic in case that's what you meant.

avatar image TimoVssn · Mar 02, 2014 at 10:35 AM 0
Share

i mean a random level of the numbers 2,3,5,6,7,8,9,10

avatar image ffxz7ff · Mar 02, 2014 at 04:00 PM 0
Share

Your real question is "how do I generate a random number within a range, excluding certain numbers". That's what you should be googling for.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by sterynkng14 · Mar 02, 2014 at 11:41 AM

This should work better.`

 var LevelOptions : int[] = [2, 3, 5, 6, 7, 8, 9, 10];
 Application.LoadLevel(LevelOptions[Random.Range(0,LevelOptions.length)]);
Comment
Add comment · Show 3 · 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 TimoVssn · Mar 02, 2014 at 12:00 PM 0
Share

it say's: array indez out of range

avatar image TimoVssn · Mar 02, 2014 at 01:06 PM 0
Share

no it doesn't work,

if i use the first one it still gives the error= array index is out of range

this is my code:

 #pragma strict
 
 var fouts : AudioClip;
 private var hit: RaycastHit;
 var LevelOptions : int[] = [2, 3, 5, 6, 7, 8, 9, 10];
 
 function Start () {
 }
 
 function Update () {
 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 if (Input.Get$$anonymous$$ouseButtonDown(0))
 {    
     if (Physics.Raycast (ray, hit , $$anonymous$$athf.Infinity))
     {
         
         if (hit.collider.tag == "goed")
         {
         Application.LoadLevel(LevelOptions[Random.Range(0,LevelOptions.length - 1)]);
         highscore.score += 1;
         }
         if (hit.collider.tag == "fout")
         {
         audio.Play();
         Application.LoadLevel("gameover");
         
         }
     }
     }
 }

and if i use the second one for levels 1 to 7 how do i use it to also load 9 but not 8?

avatar image TimoVssn · Mar 02, 2014 at 01:30 PM 0
Share

yes i get that but it doesn't work it gives me the error: array index is out of range

avatar image
0

Answer by smirlianos · Mar 02, 2014 at 10:57 AM

use this:

 var LevelOptions : int[] = [2,3,5,6,7,8,9,10];
 Application.LoadLevel(Random.Range(0, LevelOptions.length));
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 TimoVssn · Mar 02, 2014 at 11:26 AM 0
Share

that doesn't work it keeps loading level 0

avatar image TimoVssn · Mar 02, 2014 at 12:19 PM 0
Share

i looked at it but didn't get it to work

avatar image
0

Answer by Zentiu · Mar 02, 2014 at 03:57 PM

seems you need to make the script bigger with random range

just make a random range of 1 through 8 ( since you want 1 of those 8 in your array to be called)

then make like 8 if statements.

like: randomNumber = Random.Range (1,8);

 if(randomNumber == 1)
 {
     //do this.
 }
 
 if(randomNumber == 2)
 {
     //do that.
 }
 
 /ect.

better yet, make a switch statement. Switch(randomNumber): {

 case (1):
 {
  //do this.
 Break;
 }
 case (2):
 {
  //do that.
 Break;
 }
 
 //ect.


hope this helps.

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 TimoVssn · Mar 02, 2014 at 06:09 PM 0
Share

thank you this works but ins$$anonymous$$d of:

randomNumber = Random.Range (1,8);

you have to do this:

var randomNumber : int; var $$anonymous$$Ran = 1; var maxRan = 10; randomNumber = Random.Range($$anonymous$$Ran, maxRan);

(just for someone who has the same problem)

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

25 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

Related Questions

Random level select 1 Answer

random level loader? 1 Answer

how can i do that enemy cars start coming up from my 2nd level of the game ? 1 Answer

Score and Level Loading 1 Answer

help with script load level on 0 enemies 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