Unity - 单击重新启动按钮后如何重新启动分数? [英] Unity - How can I restart the score once the restart button is clicked?

查看:76
本文介绍了Unity - 单击重新启动按钮后如何重新启动分数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了问题"在正在开发的游戏中.当角色因与敌人的碰撞而死亡或触发游戏对象时,它会显示一个游戏结束屏幕,其中包含一个再试一次"按钮重新开始游戏,但点击重新开始按钮后,上一场比赛的得分仍然存在.

I am having a "problem" in a game that is being developed. When the character dies from a collision to the enemy, or trigger gameobjects, it displays a game over screen, which contains a "Try Again" button to restart the game, but the score collected from the previous game remains once the restart button is clicked.

我该如何解决这个问题?谢谢你的帮助!!

How can I solve this problem? Thank you for your help!!

GameOver C# 脚本

GameOver C# script

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

public class CS_GameOverMenu : MonoBehaviour
{

    

    public void RestartButton()
    {
        SceneManager.LoadScene("Level 1");
        Debug.Log("Game open");

    }

    public void MenuButton()
    {

        SceneManager.LoadScene("MainMenu");
        Debug.Log("Main Menu open");
    }

    public void ExitButton()
    {
        Application.Quit();
        Debug.Log("Game closed");

    }

    //public void QuitButton()
    //{
    //  Application.Quit();
    //Debug.Log("Game closed");
}

推荐答案

由于您有一个静态字段,因此即使在重置后游戏仍在运行时,这些数据仍会保留.在您的 Reset 函数内放置与此类似的代码.

As you have a static field, this data will persist while the game is still running even after a reset. Inside of your Reset function put code similar to this.

如果您不想按名称抓取对象,请使用此代码段.

If you would rather not grab the object by name, use this snippet.

public void RestartButton()
{
    ScoringSystem.theScore = 0;
    SceneManager.LoadScene("Level 1");
    Debug.Log("Game open");
}

您只需在重新加载场景时将分数设置回 0,因为您拥有的值被标记为静态.

You just need to set the score back to 0 when reloading the scene as the value you have is marked as static.

这篇关于Unity - 单击重新启动按钮后如何重新启动分数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆