加载场景时仅随机选择一次列表元素(多次)
我正在开发一个VR迷你游戏,用户在其中寻找房间内的对象。在场景的中心,有类似电视屏幕的东西,在其中显示了一个搜索对象的图像。因此,一开始,用户查看电视屏幕,以查看他们必须寻找的对象。然后,他们开始在房间内搜索此对象。一旦找到它,他们就必须按下扳机按钮,然后重新加载同一场景。重新加载后,电视屏幕上正在显示一个新对象。这样做了,直到用户至少一次搜索所有对象。
到目前为止,我所拥有的如下所示:
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;
private float timer;
[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()
{
timer += Time.deltaTime;
if (timer >= 3.0f)
{
objectsList.RemoveAt(currentObject);
objectsList[currentObject].SetActive(false);
}
}
在电视屏幕上,正在显示进行搜索的对象的图像。所有这些图像都存在于场景中,但停用了。场景开始时,我会随机选择一个图像并激活它。这样,正在显示待办事项对象的图像,并提示用户要查找的对象。
我有几个问题:
- 尽管列表在场景首次加载时填充了,但从第二个负载开始,检查员中的列表元素是空的(“丢失(gameObject)”)。但是,每次我重新加载场景时,图像都会正确显示。
- 尽管我试图摆脱以前选择的图像,因此它们不再显示,但我不正确。每次都会显示随机图像,但这意味着有些图像可能会出现2或3次,而不是1次。
任何建议都会非常受欢迎。
感谢您的时间。
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;
private float timer;
[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()
{
timer += Time.deltaTime;
if (timer >= 3.0f)
{
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:
- 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.
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是问题出在哪里的主要指标: 但是,每次我重新加载场景时,图像都会正确显示。
什么代码使脚本在负载?
你有一个单身人士!这很好,但是要记住这种模式的东西是,该脚本的第一个负载是唯一可以在您的游戏生活中持续存在的脚本。所有未来的Sceneloads都会创建自己的脚本实例,但在执行逻辑之前会破坏自身。
这意味着
start()
不会在场景的任何未来负载上运行,因为原始脚本是仍然处于活动状态的脚本。您的预期行为在这里不清楚,因此我无法提供精确的代码,但是您可能需要将start()
逻辑移动到其他地方,以确保其每次加载时运行。您可能无法访问正确的对象清单。我在这里看不到对实例的任何引用,也无法根据您的结论得出结论。
Here's the big indicator of what the problem is: However, the images are being displayed properly each time I reload the scene.
What lines of code are making the script behave differently between loads?
You've got a singleton! That's perfectly fine, but something to remember with this pattern is that the first load of this script is the only one that will persist through the life of your game. All future sceneloads will create their own instance of this script, but will destroy itself before it can perform logic.
This means
Start()
will not run on any future loads of the scene, because the original script is the one that is still active. Your intended behavior is unclear here so I can't provide exact code, but you'll likely want to move yourStart()
logic elsewhere to ensure it runs every time it loads.You're likely not accessing the correct objectList. I don't see any references to the instance here, and can't draw a conclusion based on what you have here.