简体   繁体   English

我正在努力让我的 Unity 游戏在游戏结束后重新启动

[英]I am trying to make it so that my Unity game restarts after the game has ended

I don't know how to reload a scene so that the user can press a key and reset the game.我不知道如何重新加载场景,以便用户可以按键并重置游戏。 Whenever I do it, the engine just crashes.每当我这样做时,引擎就会崩溃。 The game is based on a cat chasing glasses around with a 120-second timer, and each of them has unique abilities.游戏是基于一只猫追逐眼镜,有一个 120 秒的计时器,每个人都有独特的能力。 On collision, it should reload the scene.碰撞时,它应该重新加载场景。 Is there any way that I can do this?有什么办法可以做到这一点吗?

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

public class gamemanager : MonoBehaviour
{
    public GameObject catwin;
    public GameObject glasseswin;
    public timer timer;
    public Transform catpos;
    public Transform glassespos;
    public Vector3 catspawn;
    public Vector3 glassesspawn;
    public bool gameover = false;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey("r"))
        {
            GameReset();
        }
    }

    public void CW()
    {
        catwin.SetActive(true);
        gameover = true;
        while (gameover)
        {
            if (Input.GetKey(KeyCode.R))
            {
                GameReset();
            }
        }
    }

    public void GW()
    {
        glasseswin.SetActive(true);
        if (Input.GetKey("r"))
        {
            GameReset();
        }
    }

    public void GameReset()
    {
        catwin.SetActive(false);
        glasseswin.SetActive(false);
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
}

It crashes because the while loop inside the CW method is actually a while (true).它崩溃是因为 CW 方法中的 while 循环实际上是一个 while (true)。 When something calls this method it should wait until method is completed, but it'll never happen because of the loop.当有东西调用这个方法时,它应该等到方法完成,但由于循环,它永远不会发生。 As a result, the program gets stuck while trying to execute infinitely many commands in one frame, so it has no other choice but to crash.结果,程序在试图在一帧中执行无限多的命令时卡住了,所以它别无选择,只能崩溃。

In order to fix it remove the loop from the method and check whether the key was pressed in Update.为了修复它,从方法中删除循环并检查是否在更新中按下了键。 Something like this:像这样的东西:

if (gameover && Input.GetKey("r")) 
    GameReset();

You can replace gameover with something else in order to include other conditions您可以用其他东西替换gameover以包含其他条件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试使用 Unity 制作 FPS 足球游戏,但我的脚本不起作用 - I am trying to make an FPS soccer game using Unity, but my script isn't working 我正在尝试为我的 Unity 游戏编写一个重置按钮 - I am trying to code a reset button for my Unity game 我正在尝试统一构建游戏,但这给了我一个错误 - I am trying to build my game in unity but it is giving me an error 失去焦点后游戏重新启动 Unity iOS - Game restarts after lose focus Unity iOS 我正在用统一引擎制作 2d 平台游戏,但我无法让我的玩家跳跃 - I am making a 2d platformer game in unity engine, but I am unable to make my player jump 我正在做一个统一游戏,我的球在击中 object 后一直漂移到一边 - I am making a unity game and my ball keeps drifting to the side after it hit an object 我正在努力使玩游戏的人可以在触发器中按他们想要的次数按“E” | Unity 3d + 动画师 - I'm trying to make it so the person playing the game can press "E" as much as they want in the trigger | Unity 3d + animator 如何使克隆的游戏对象成为全局对象,以便可以在Unity功能之外使用它们? - How do I make my cloned game objects global, so I can use them outside of the function in Unity? 重新启动游戏后,GameObjects将不会加载 - GameObjects will not load after game Restarts 尝试统一制作一个简单的Tag游戏 - Trying to make a simple Tag game in unity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM