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;
}
}
}