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 kim nana · Oct 01, 2014 at 04:13 AM · c#scenerandom

How to load a scene level randomly without repeating

Hi, currently I am working for my school's project. The issue is I try to load the scene randomly. When I click on the "Button to the Next Level" in the current scene, it works but the scenes in the list keep repeating. What I want is when the scene has load at once, so the current scene will be removed from the list. I am expected that the scenes will be load randomly whenever I click on the "Button to the Next Level" without repeating the previous scene until the list of the scenes is empty. This is the coding that I have tried so far:

 using UnityEngine;
 using System.Collections;
 using System.Linq;
 using System.Collections.Generic;
 
 public class NextLevel : MonoBehaviour
 {
     public Transform tweenTarget;
     public Vector3 hover = new Vector3(1.1f, 1.1f, 1.1f);
     public Vector3 pressed = new Vector3(1.05f, 1.05f, 1.05f);
     public float duration = 0.2f;
     Vector3 mScale;
     bool mStarted = false;
     bool mHighlighted = false;
 
 
     ////max number of load scene is 10
     protected const int MAX = 10;
     private List<int> scenes = new List<int>();
     
     void Start()
     {
         if (!mStarted)
         {
             mStarted = true;
             if (tweenTarget == null) tweenTarget = transform;
             mScale = tweenTarget.localScale;
             
                // Initialize the list with levels
             scenes = new List<int>(Enumerable.Range(1,MAX));
         }
     }
     
     void OnEnable()
     { if (mStarted && mHighlighted) OnHover(UICamera.IsHighlighted(gameObject)); }
     
     void OnDisable ()
     {
         if (mStarted && tweenTarget != null)
         {
             TweenScale tc = tweenTarget.GetComponent<TweenScale>();
 
             if (tc != null)
             {
                 tc.scale = mScale;
                 tc.enabled = false;
             }
         }
     }
 
     void OnPress (bool isPressed)
     {
         if (enabled)
         {
             if (!mStarted) Start();
             TweenScale.Begin(tweenTarget.gameObject, duration, isPressed ? Vector3.Scale(mScale, pressed) :
                 (UICamera.IsHighlighted(gameObject) ? Vector3.Scale(mScale, hover) : mScale)).method = UITweener.Method.EaseInOut;
         }

             //No scenes left in the list, so exit
             if(scenes.Count == 0)
             {
                 Application.LoadLevel("QuitScene");
             }
 
             // Get a random index of the remaining scenes in the list
             int randomIndex = Random.Range(0, scenes.Count);
             int level = scenes[randomIndex];
             scenes.RemoveAt(randomIndex); // Removes the level from the list
             AutoFade.LoadLevel(level, 1,1,Color.black);
 
             isPressed = false;
     }
 
     void OnHover (bool isOver)
     {
         if (enabled)
         {
             if (!mStarted) Start();
             TweenScale.Begin(tweenTarget.gameObject, duration, isOver ? Vector3.Scale(mScale, hover) : mScale).method = UITweener.Method.EaseInOut;
             mHighlighted = isOver;
         }
     }
 }
 
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
0

Answer by Kiwasi · Oct 01, 2014 at 05:26 AM

I would use DontDestroyOnLoad and a singleton pattern.

You could also make the list static.

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 kim nana · Oct 01, 2014 at 07:05 AM 0
Share

I never go through on singleton pattern. If that so, I'll try to study on it. Thank you for your feedback.

avatar image
0

Answer by PAHeartBeat · Oct 01, 2014 at 07:09 AM

You can use prbebilty system like

 private List<int> probability = new List<int>() { 0, 1, 2 };
 private List<int> tempProbability = new List<int>();
 private List<string> sceneNames = new List<string>() { "Level1", "Level2", "Level3" };
 
 void LoadNewScene() {
     if(tempProbability.Count <= 0) {
         tempProbability.AddRange(probability.ToArray());
     }
     int a = UnityEngine.Random.Range(0, tempProbability.Count);
     int nextScene = tempProbability[a];
     tempProbability.RemoveAt(a);
     Application.LoadLevel(sceneNames[nextScene]);
 }

Check this one it will not repeast same scene load next time. it laways load different scene from current one Put this code in Object which are Don't destroy on load, and write code on OnGUI() with Button to call this code

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 kim nana · Oct 01, 2014 at 04:57 PM 0
Share

ohhh thanks so much. better for me to understand. i have tried and it works. Sometime, there is a bit problem when I try to put the coding in the object. The scene still keep repeating, and I'm not sure what's wrong.

avatar image Kiwasi · Oct 01, 2014 at 06:47 PM 0
Share

This solution will do the random level generation. But you still need to make it persist between scenes.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

[C#] Is it possible to instantiate a scene? 3 Answers

Distribute terrain in zones 3 Answers

Hi, how would I go about spawning a certain number of items at random positions in a scene? 0 Answers

Why is this not giving me an offset 1 Answer


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