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);
}
}
}
,
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);
}
}
}
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?