C# 无法从Unity中的主场景加载多个场景?
我的游戏性是这样的,我希望我的场景执行以下流程:C# 无法从Unity中的主场景加载多个场景?,c#,unity3d,htc-vive,C#,Unity3d,Htc Vive,我的游戏性是这样的,我希望我的场景执行以下流程: 场景1->场景2->场景1 场景1->场景3->场景1 场景1->场景4->场景1 场景1->场景5->场景1 我有4个不同的触发器导致4个不同的场景。但每次我启动Scene1时,第一次接触任何触发器时被实例化的场景,不管下一个触发器指向哪个场景,都会再次被实例化。 我做错了什么? 如何解决? 您在上图中看到的碰撞器是4个触发器。每个触发器上的代码如下所示: using System.Collections; using System.Co
- 场景1->场景2->场景1
- 场景1->场景3->场景1
- 场景1->场景4->场景1
- 场景1->场景5->场景1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class enter4kWorld : MonoBehaviour {
//Orb Transform
public GameObject targetGO;
public GameObject otherTargetGO1;
public GameObject otherTargetGO2;
public GameObject otherTargetGO3;
public Animator LightBurst;
public GameObject Processor;
public GameObject SceneExitGO;
public float CountDownTimer;
// Use this for initialization
void Start () {
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "GameController")
{
Debug.Log("Teleporter works");
SceneExitGO.SetActive(true);
targetGO.SetActive(true);
otherTargetGO1.SetActive(false);
otherTargetGO2.SetActive(false);
otherTargetGO3.SetActive(false);
}
}
// Update is called once per frame
void Update()
{
if (SceneExitGO.activeSelf)
{
//float step = speed * Time.deltaTime;
//transform.position = Vector3.MoveTowards(transform.position, target.position, step);
CountDownTimer -= Time.deltaTime;
if (Processor.transform.position.y <= 9f)
{
Processor.transform.Translate(Vector3.up * Time.deltaTime * 1.5f, Space.World);
}
if (CountDownTimer <= 4.5)
{
LightBurst.SetTrigger("TriggerLightBurst");
}
if (CountDownTimer <= 0)
{
ChangeScene();
CountDownTimer = 0;
}
}
}
public void ChangeScene()
{
SceneManager.LoadScene("Scene2");
//SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.SceneManagement;
世界公共阶层:单一行为{
//球变换
公共游戏对象targetGO;
公共游戏对象otherTargetGO1;
公共游戏对象otherTargetGO2;
公共游戏对象otherTargetGO3;
公共动画师光爆;
公共游戏对象处理器;
公共游戏对象场景退出;
公共浮点倒计时;
//用于初始化
无效开始(){
}
无效对撞机(对撞机其他)
{
if(other.tag==“GameController”)
{
Log(“传送机工作”);
sceneexigo.SetActive(true);
targetGO.SetActive(真);
otherTargetGO1.SetActive(假);
otherTargetGO2.SetActive(假);
otherTargetGO3.SetActive(假);
}
}
//每帧调用一次更新
无效更新()
{
if(SceneExitGO.activeSelf)
{
//浮动步长=速度*时间.deltaTime;
//transform.position=Vector3.movetoward(transform.position,target.position,step);
倒计时-=Time.deltaTime;
如果(Processor.transform.position.y为自己编写一个真正的触发器,在回车时触发UnityEvent
有一个单独的单一行为,开始新的场景
在编辑器中,只需将上面提到的场景开始组件拖到UnityEvent上,并让事件调用加载下一个场景的方法。类似如下:
public class Trigger : MonoBehaviour
{
public UnityEvent OnEnter;
[SerializeField]
private string tag;
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag(tag) && OnEnter != null)
OnEnter.Invoke();
}
}
下面是要调用的类:
public class SceneManagement : MonoBehaviour
{
public void LoadScene(string name)
{
SceneManager.LoadScene(name);
}
}
注意:我不保证这是没有错误的,我没有检查它是否有效,我只是根据经验写下来。但我希望你能理解这个概念。多亏了一位朋友,我来解决这个问题。这里的问题是,所有附加到所有触发器的脚本都是相似的,如果仔细观察,它们都包含相同的public游戏对象,即SceneExitGO。一旦SceneExitGO激活,连接到每个触发器的所有脚本将被激活,以在更新功能中执行各自的操作。这种情况称为“竞争条件”,当多个组件或脚本同时运行以获得结果时,会发生冲突。我只是通过将公共游戏对象更改为私有对象来解决此问题。这些触发器是什么?它们是如何工作的?请编辑您的问题以包含任何相关代码或解释。您确定不使用SceneManager.Lo吗adScene(sceneBuildIndex,LoadSceneMode.Additional);
某个地方?否则,场景不应该“再次实例化”。也很难在不发布代码的情况下对其进行调试。@我已经共享了代码以及碰撞器的图片。上面所有的触发器都有类似的脚本,可以打开不同的场景。但不幸的是,只有一个特定的场景正在打开,无论我在设置构建集后第一次播放哪个场景卡杜克斯我也试过用它,但没用。