首页 > 解决方案 > 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;

            };
        } 
}

这是我运行游戏时发生的情况:

https://youtu.be/zwEANZhVeV8

这是视频

尽快答复

:D

标签: c#unity3d

解决方案


尝试制作一个单例脚本的 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";
}

推荐阅读