Unity3d C#重新发布

本文关键字:新发布 Unity3d | 更新日期: 2023-09-27 18:22:15

如果我在Unity中测试我的游戏并重生,我将恢复我的3条命。但当我构建游戏并重生时,我只恢复了2条生命。

这是我的代码(不是完整的代码),我用于重新部署:

public int StarterLives; // 3
public GameObject plr; // My Player
public float maxVoidDist; // -10
public Object respawnLevel; // Level01 (My first and only asset)
public Text LivesHolder; // The text object (UI)
private Vector3 respawnPoint; // This gets updated in Start() and becomes the first position of the player
private string deadLevel; // This gets updated in Start() and becomes the name of my respawnlevel
private int lives; // This gets updated in Start() and becomes StarterLives (3)
private bool delay1 = false;

void Update () {
    if ((plr.transform.position.y <= maxVoidDist) && (delay1.Equals(false)))
    {
        delay1 = true;
        if((lives - 1) <= 0)
        {
            Application.LoadLevel(deadLevel);
            lives = StarterLives + 1;
        } else
        {
            plr.transform.position = respawnPoint;
        }
        lives = lives - 1;
        updateLives();
        delay1 = false;
    }
}
void updateLives()
{
    LivesHolder.text = "Lives: " + lives;
}

完整代码:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GameController : MonoBehaviour {
    public int StarterLives;
    public GameObject plr;
    public float maxVoidDist;
    public Object respawnLevel;
    public Text LivesHolder;
    private Vector3 respawnPoint;
    private string deadLevel;
    private int lives;
    private bool delay1 = false;
    void Awake()
    {
        QualitySettings.vSyncCount = 1;
    }
    // Use this for initialization
    void Start () {
        respawnPoint = plr.transform.position;
        deadLevel = respawnLevel.name;
        lives = StarterLives;
        updateLives();
    }
    // Update is called once per frame
    void Update () {
        if ((plr.transform.position.y <= maxVoidDist) && (delay1.Equals(false)))
        {
            delay1 = true;
            if((lives - 1) <= 0)
            {
                Application.LoadLevel(deadLevel);
                lives = StarterLives + 1;
            } else
            {
                plr.transform.position = respawnPoint;
            }
            lives = lives - 1;
            updateLives();
            delay1 = false;
        }
    }
    void updateLives()
    {
        LivesHolder.text = "Lives: " + lives;
    }
}

Unity3d C#重新发布

我在你的代码中看到了一些奇怪的东西,我希望它们能代表这个问题:

1)

if((lives - 1) <= 0)

通过这个测试,如果你还有1个生命,你将重新启动关卡。这是你想要的吗?

2)

Application.LoadLevel(deadLevel);
lives = StarterLives + 1;

在这个片段中,第二行是无用的,因为一旦调用LoadLevel(),就会加载新场景,并且不会执行其余代码。因此,lives = StarterLives + 1;是死代码。

3) 关于第二点,让我们假设这些线的顺序是颠倒的(所以,它们的顺序是"正确的")。您似乎正在尝试更新一些值,以便在级别重新启动时拥有这些值。但是我在您的代码中看不到DontDestroyOnLoad,所以值的保存是无用的。

希望这能有所帮助!