- Home /
Loading a scene based on a generated number
I have a script that randomly generates a number (1-16) everytime the game is started, and a script that causes the screen to fade to black after the player enters a trigger and disables them from moving. During this fade to black, I want to look at what number was generated, and if the number was "8" and ONLY "8" it will load a scene where the player has to run from an enemy. If the number is anything else besides "8" (1-7 or 9-16) it will load a different scene where the player is then chasing the enemy
var number = 0;
function Start(){
number = Random.Range(1,17);
print(number);
}
that is the script for the number generator. I just need for it to load a scene based on if the number is "8" or anything else (see above if you are confused), as soon as the player steps onto a trigger I have set up @akisrn
Answer by Harinezumi · Oct 27, 2017 at 12:30 PM
This seems simple, so I'm probably missing something, but this is what I would do:
private void Start() {
int number = Random.Range(1,17);
if (number == 8) { SceneManager.LoadScene("ChasePlayerScene"); }
else { SceneManager.LoadScene("ChaseEnemyScene"); }
}
Name your scenes just like in the LoadScene() functions and add them to your build. Alternatively, call them whatever you want, and add one scene with one index, the other with another, and pass the correct index instead of the name in the LoadScene().
Also, if you want this to happen when the player steps into a door, create an invisible Collider in the door, mark it as trigger, then attach the above script, and change the name of the function to OnTriggerEnter(Collider other).
Note that this is C# script, Unity is abandoning support for UnityScript (also called JScript).
Would this go on my FadeTrigger script or under my RandomNumberGenerator script? I already have an OnTriggerEnter for my FadeTrigger script.
I plugged in the code and there seems to be a problem. Whenever the player enters the trigger it loads the ChaseEnemyScene even if the number that was generated was '8'
this is my full code: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Scene$$anonymous$$anagement;
public class TriggerFade : $$anonymous$$onoBehaviour {
public GameObject player;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
player.GetComponent<FadeOut>().enabled = true;
int number = Random.Range(1,17);
if (number == 8)
{
Scene$$anonymous$$anager.LoadScene("ChasePlayerScene");
}
else
{
Scene$$anonymous$$anager.LoadScene("ChaseEnemyScene");
}
}
}
}
Idk if it's part of the problem, but before when I tried running the script it said that the scenes had to be in the build settings so I added both scenes. So now it's Lottery, ChaseEnemyScene, ChasePlayerScene. I tried changing up the order of ChaseEnemyScene and ChasePlayerScene so that ChasePlayerScene is first but even then the only scene that would load would be ChaseEnemyScene
How do you know it's loading the wrong scene when the generated number is 8? Try adding a Debug.Log to show you what number is, just after initialising it, and/or testing that particular pathway by temporarily changing the initialisation to int number = 8;
I do have something that tells me what number was generated, it's just on another script
var number = 0;
function Start(){
number = Random.Range(1,17);
print(number);
}
And I've spammed through the play button until I get '8' and when I check on the heirarchy it always loads "ChaseEnemyScene" when it should be loading "ChasePlayerScene" when the NumberGeneratorScript loads an '8' @Bonfire-Boy
Did you solve the problem? You marked the question answered, but I don't see what the issue was, the code that you posted should work (assu$$anonymous$$g you have the scenes added to builds with the correct names, the player GameObject set (although I would use the Collider other).
The only issue I see is that the var number and the int number in OnTriggerEnter() are NOT the same variable: the first is a member variable of the class, while the second is a local variable of the function. This means if you print the value of the member number the local variable can be completely different.
Answer by akisrn · Oct 27, 2017 at 04:47 AM
Can you post a preview of your script? You can use a switch case for this and just load whatever scene you need to depending on what case is true.
I'll post it right now! just give me a $$anonymous$$ute
Your answer
