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 christian24689000 · Oct 27, 2017 at 01:15 AM · scene-loadingrandom.rangescene-changenumber

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

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
1
Best Answer

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).

Comment
Add comment · Show 6 · 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 christian24689000 · Oct 28, 2017 at 03:10 AM 0
Share

Would this go on my FadeTrigger script or under my RandomNumberGenerator script? I already have an OnTriggerEnter for my FadeTrigger script.

@Harinezumi

avatar image christian24689000 · Oct 28, 2017 at 04:24 AM 0
Share

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

@Harinezumi

avatar image Bonfire-Boy christian24689000 · Oct 29, 2017 at 01:34 AM 0
Share

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;

avatar image christian24689000 Bonfire-Boy · Oct 29, 2017 at 04:54 PM 0
Share

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

Show more comments
avatar image Harinezumi christian24689000 · Oct 29, 2017 at 10:45 PM 0
Share

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.

avatar image
0

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.

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 christian24689000 · Oct 27, 2017 at 11:51 AM 0
Share

I'll post it right now! just give me a $$anonymous$$ute

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

75 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

Related Questions

Using parameters of activeSceneChanged? 0 Answers

FixedUpdate is not called after loading a level for the second time 1 Answer

Is there any ideas to load Unity scene asynchronously? 1 Answer

Random.Range(..) not working 1 Answer

Unity Scene loading problem (slow loading until it freezes) 0 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