- Home /
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?
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.
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.
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)]);
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?
yes i get that but it doesn't work it gives me the error: array index is out of range
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));
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.
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
Follow this Question
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