gpt4 book ai didi

c# - 用户界面统一错误 : The requested operation caused a stack overflow

转载 作者:太空宇宙 更新时间:2023-11-03 14:49:32 25 4
gpt4 key购买 nike

所以我制作了这个测试 GUI,当敌人的聚光灯照到你时,它会显示游戏结束。但是当它发生时 Unity 崩溃并注意到错误

StackOverflowException: The requested operation caused a stack overflow. 

这是我崩溃的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;


public class GameUI : MonoBehaviour {

public GameObject gameLoseUI;
// public GameObject playerDiedUI;
//public GameObject noEnergyUI;
bool gameIsOver;

// Use this for initialization
void Start () {
Guard.OnGuardHasSpottedPlayer += ShowGameLoseUI;
}

// Update is called once per frame
void Update () {
/*if (gameIsOver)
{if (Input.GetKeyDown(KeyCode.Space))
{
SceneManager.LoadScene(0);
}
}*/
}

void ShowGameLoseUI()
{
OnGameOver(gameLoseUI);
}

void OnGameOver(GameObject gameoverUI)
{
gameLoseUI.SetActive(true);
gameIsOver = true;
Guard.OnGuardHasSpottedPlayer += ShowGameLoseUI;
}
}

我认为错误可能在 void Update 中,但即使使用此注释片段,unity 仍会崩溃。我该如何解决这个问题?

最佳答案

正如评论中所指出的,您在 GameOver 时无限订阅 Guard.OnGuardHasSpottedPlayer,这会导致异常。

+=改为-=

void OnGameOver(GameObject gameoverUI)
{
gameLoseUI.SetActive(true);
gameIsOver = true;
Guard.OnGuardHasSpottedPlayer -= ShowGameLoseUI;
}

关于c# - 用户界面统一错误 : The requested operation caused a stack overflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52378340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com