简体   繁体   English

正确重置场景并保持Unity得分

[英]Correctly reset Scene and keep score in Unity

So I am making a game in Unity in order to learn how to code, and I have kind of hit a wall again. 因此,我正在Unity中制作游戏,以学习编码方法,而我又碰壁了。 The game is a "dodge the falling objects game", and the way I am creating the objects is as follows: 该游戏是“躲避掉落的物体游戏”,我创建物体的方式如下:

public class Salt : MonoBehaviour {
    public float delay;
    public GameObject salt;

    // Use this for initialization
    void Start () {
        InvokeRepeating("Spawn", delay, delay);
    }
    // Update is called once per frame
    void Spawn () {
        Instantiate(salt, new Vector3(Random.Range(-2.5f, 2.5f), 10, 0), Quaternion.identity);
    }
}

This part of the code seems to be working fine. 这部分代码似乎工作正常。 The problem occurs when the salt (falling objects) make contact with the player. 当盐(掉落的物体)与播放器接触时,会发生此问题。 The game resets like it should, however the salt that was on the screen already is still there and I can't figure out how to delete all of the clones that have already been made. 游戏会像应有的那样重置,但是屏幕上已经存在的盐仍然存在,我无法弄清楚如何删除所有已经制作的克隆。 Basically I can't figure out how to clear the objects that have already been created when the player loses. 基本上,我不知道当玩家失败时如何清除已经创建的对象。

The way I reset the game after collision: 碰撞后我重设游戏的方式:

public class TapToStart : MonoBehaviour {

    public Text highScore;
    public Button startButton;
    public ScoreManager sMan;

    // Use this for initialization
    void Start () {
        Init();
    }

    public void Init() {
        Time.timeScale = 0;
        sMan.SetScore(0);
        highScore.text = "High Score: " + Mathf.Round(sMan.GetHighScore()).ToString();
        startButton.gameObject.SetActive(true);
    }



    public void Pressed() {
        startButton.gameObject.SetActive(false);
        Time.timeScale = 1;
    }
}

I thought maybe making a SaltManager object would work, and then just have the Instantiated salt objects be children of it would work so that I could just delete the SaltManager after and all the children would disappear as well, however I can't figure out how to do that and it doesn't seem too efficient anyways. 我以为也许让SaltManager对象起作用,然后让Instantiated盐对象成为它的子对象就可以了,这样我就可以删除SaltManager,然后所有子对象也消失,但是我不知道怎么做这样做,反正似乎并不太有效。 Any help would be greatly appreciated! 任何帮助将不胜感激! Thanks! 谢谢! -Brandon 布兰登

I would recommend that you reset the game by reloading the scene so like that you get it exactly how it was at the beginning. 我建议您通过重新加载场景来重置游戏,以使它完全像开始时一样。 You can load your scene like that : 您可以像这样加载场景:

SceneManager.LoadScene("SceneName");

Or with the scene index : 或与场景索引:

SceneManager.LoadScene(sceneIndex); 

I can see that you're keeping a highscore and its probably why you didn't do it that way. 我可以看到您保持着高分,这可能就是为什么您没有那样做的原因。 You should save your high score with PlayerPrefs like this : 您应该这样保存PlayerPrefs的高分:

PlayerPrefs.SetInt("High Score", 10);

Then you can easily get it back with : 然后,您可以轻松地将其恢复为:

PlayerPrefs.GetInt("High Score");

This actually saves your score so that even if they quit the game and come back you can still retrieve it. 这实际上可以保存您的得分,这样即使他们退出游戏并回来了,您仍然可以找回它。 More info on PlayerPrefs . 有关PlayerPrefs的更多信息

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM