c# - Unity 出现故障但不显示任何错误
问题描述
Unity 不执行任何操作,我的游戏音频无法正常工作,但控制台中没有错误。
我认为我的代码可能会导致这种情况。
这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class CompDel : MonoBehaviour
{
public Text TimeText;
public float Timer = 70;
public string currentScene = "";
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Timer = Timer - 1 * Time.deltaTime;
if (Timer < 40)
{
this.GetComponent<RotateAround>().enabled = false;
};
if (Timer < 0 && currentScene != "AESCENE")
{
SceneManager.LoadScene("AESCENE");
currentScene = "AESCENE";
}
else if (Timer < 30 && currentScene != "MainScene")
{
SceneManager.LoadScene("MainScene");
currentScene = "MainScene";
}
else if (Timer < 40 && currentScene != "SSScene")
{
SceneManager.LoadScene("SSScene");
currentScene = "SSScene";
}
if (Timer < 0)
{
this.GetComponent<RotateAround>().enabled = false;
this.GetComponent<RR1>().enabled = false;
};
}
}
这是我运行游戏时发生的情况:
这是视频
尽快答复
:D
解决方案
尝试制作一个单例脚本的 SceneManager,一旦场景加载计时器重置并且脚本再次从 70 开始,该loadscene
方法将被一遍又一遍地调用。您将必须制作一个脚本,将其附加到一个空gameobject
并使其进入内部DontDestroyOnLoad
。请阅读更多关于使用单例的信息。
为什么你的末尾有分号if
?
if (Timer < 40)
{
this.GetComponent<RotateAround>().enabled = false;
};
最后如果statement
也是。
您可以稍微清理一下代码:
if (Timer < 0)
{
if(currentScene != "AESCENE")
{
SceneManager.LoadScene("AESCENE");
currentScene = "AESCENE";
}
else
{
this.GetComponent<RotateAround>().enabled = false;
this.GetComponent<RR1>().enabled = false;
}
}
else if (Timer < 30 && currentScene != "MainScene")
{
SceneManager.LoadScene("MainScene");
currentScene = "MainScene";
}
else if (Timer < 40 && currentScene != "SSScene")
{
SceneManager.LoadScene("SSScene");
currentScene = "SSScene";
}
推荐阅读
- python - Anaconda 虚拟环境 Python 脚本未执行
- github - 如何删除 GitHub 页面子域重定向?
- kubernetes - Redis - 使用集群避免数据丢失(使用 Gossip 协议)
- ios - USDZ Safari 权限
- javascript - Bootstrap 4 Navbar 切换无法隐藏(ruby on rails 应用程序)
- javascript - CSS - 动画整个 ul 而不仅仅是新项目进入时的第一行
- python - 使用服务帐户密钥的 Google Drive Python API 授权
- ruby - 用逗号分割字符串,但不包括括号中的逗号
- xml - XQuery 从每个 XML 记录的名称更改的元素节点的子节点中检索值
- javascript - 实施 Oauth。与第三方进行身份验证后应该将使用重定向到哪里?