- Home /
Select element from list only once when reloading new scene
Hello everyone,
I'm developing a VR mini game in which the user looks for objects inside a room. At the center of the scene there is something like a tv screen, where an image of the to-be-searched object is being displayed. So, at the beginning the user takes a look at the tv screen to see which object they have to look for. Then, they start searching within the room for this object. Once they find it, they have to press the trigger button and the same scene is reloaded. After reloading, a new object is being displayed on the tv screen. This goes on until the user searches for all the objects at least once.
What I have until now is shown below:
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class TrialHandler : MonoBehaviour
{
public static TrialHandler Instance;
public List<GameObject> objectsList;
private int currentObject;
[HideInInspector] public static int trialNum = 0;
private void Awake()
{
objectsList = objectsList.Where(item => item != null).ToList();
if (Instance = null)
{
Instance = this;
}
else if (Instance != null)
{
Destroy(gameObject);
return;
}
DontDestroyOnLoad(gameObject);
}
void Start()
{
currentObject = Random.Range(0, objectsList.Count);
objectsList[currentObject].SetActive(true);
trialNum += 1;
}
void Update()
{
objectsList.RemoveAt(currentObject);
objectsList[currentObject].SetActive(false);
}
On the tv screen, images of the to-be-searched objects are being displayed. All these images are present in the scene but deactivated. When the scene starts, I select one image randomly and activate it. This way an image of the to-be-searched object is being displayed and prompts the user on which object to look for.
I have several problems: 1. Although the list becomes populated the first time the scene loads, from the second load onwards the list elements in the inspector are empty ( "Missing (GameObject)"). However, the images are being displayed properly each time I reload the scene. 2. Despite the fact that I am trying to get rid off the image(s) that were previously selected, so they don't get displayed again, I don't get it right. Random images are getting displayed each time, but this means that some might appear 2 or 3 times, instead of 1.
Any advice would be very much welcomed.
Thanks for your time.
Answer by rh_galaxy · 5 days ago
On a quick look I see that you assign null to Instance on line 17...
This is my own code for a singleton DontDestroyOnLoad GameManager.
void Awake()
{
//singleton
if (Instance== null)
{
Instance = this;
}
else if (Instance != this)
{
//enforce singleton pattern, meaning there can only ever be one instance of a GameManager.
Destroy(gameObject); //<- this makes OnDestroy() be called and we don't
// want to deinit everything there, be careful
return;
}
//the rest is done once only...
DontDestroyOnLoad(gameObject);
//do other initialization like populate the list and so on
//...
}
There might be other problems, but you can start testing this.