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 /
  • Help Room /
This question was closed Apr 14, 2019 at 01:16 AM by Marian10 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Marian10 · Apr 11, 2019 at 06:57 PM · loadingrandom.range

Loading random scenes, but never the same

Hello guys, I am pretty much a total beginner with Unity and I am currently trying to figure out a script for a VR experiment I am trying to create. I already managed to randomize the scenes and implement a timer. Now my question is, how can I prevent the same scene from appearing twice and make it so that one run only goes through every scene once? This is my code so far:

 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class LoadingAfterTime : MonoBehaviour
 {
     [SerializeField]
     private float delayBeforeLoading = 10f;

   
 private float timeElapsed;

 private void Update()
 {
     timeElapsed += Time.deltaTime;

     if (timeElapsed > delayBeforeLoading)
     {
         int index = Random.Range(1, 4);
         SceneManager.LoadScene(index);
     }
 }

} ,

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

  • Sort: 
avatar image
2
Best Answer

Answer by Hellium · Apr 11, 2019 at 09:15 PM

  using UnityEngine;
  using UnityEngine.SceneManagement;
  using System.Collections.Generic;
  
  public class LoadingAfterTime : MonoBehaviour
  {
     private static List<int> scenes = new List<int>(){ 1, 2, 3, 4 } ;
     
     [SerializeField]
     private float delayBeforeLoading = 10f;
    
     private float timeElapsed;

     private void Start()
     {
         if( scenes.Count <= 0 )
         {
             Debug.LogWarning( "No remaining scene to load" ) ;
             enabled = false;
         }
     }

     private void Update()
     {
         timeElapsed += Time.deltaTime;
         if (timeElapsed > delayBeforeLoading)
         {
              int index = Random.Range(0, scenes.Count);
              int scene = scenes[index];
              scenes.RemoveAt( index ) ;
              SceneManager.LoadScene(scene);
         }
     }
 }
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 Marian10 · Apr 11, 2019 at 11:43 PM 0
Share

Thank you for the quick response, really appreciated. Unfortunately I get the message "Cannot implicitly convert type float' to `int'. An explicit conversion exists (are you missing a cast?)". Any explanation why this happens?

avatar image Marian10 Marian10 · Apr 11, 2019 at 11:48 PM 0
Share

okay just added (int) to the "int scene = (int)scenes[index];" row and it magically works, hope that was right :) Thanks a lot again for your effort!

avatar image Hellium Marian10 · Apr 12, 2019 at 04:59 AM 1
Share

$$anonymous$$y bad, I don't know why I declared scenes as a List of floats, ins$$anonymous$$d of a list of integers. Fixed it so you don't have to cast.

avatar image Marian10 Hellium · Apr 13, 2019 at 02:11 PM 0
Share

I don't want to harrass you, but if I want some end scene to play after the last scene has played, how would I go about that? I tried to just add Scene$$anonymous$$anager.Loadscene("scenename") to the conditional scenes.Count statement, but everytime it loads the last scene, it would just immediately skip to the end scene without delay. Do i just need to add another scenes.Count statement but with another value, if that makes sense?

Show more comments

Follow this Question

Answers Answers and Comments

170 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 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 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

How to switch to next level after completeing the task in current level 6 Answers

Unity 5.0.2 - How can I know when a scene is loaded AND when it's running well? 0 Answers

Resources.Load taking very long on Android 0 Answers

Unity Editor Not Starting/Not Responding 1 Answer

Async Level loading issue. 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