- Home /
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;
}
}
}
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.
I never go through on singleton pattern. If that so, I'll try to study on it. Thank you for your feedback.
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
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.
This solution will do the random level generation. But you still need to make it persist between scenes.