简体   繁体   English

如何在 C#/unitygames 中加载死亡场景?

[英]How to load death scene in C#/unitygames?

haven't posted on here before but I have been trying for a while to create a game and would like a death/game over sort of scene to appear when the player loses all 3 of their lives.之前没有在这里发布过,但我已经尝试了一段时间来创建游戏,并且希望在玩家失去所有 3 条生命时出现死亡/游戏场景。 I have a functioning game manager and my player can lose lives (they have 3).我有一个正常运作的游戏经理,我的玩家可能会丧命(他们有 3 条命)。 This is all being done in unity games and is 2d (idk if that helps).这一切都是在统一游戏中完成的,并且是 2d(如果有帮助的话,idk)。 I currently have other stuff in my scene loader script that works fine so I will post the whole thing but I am having issues with the bottom most code!我目前在我的场景加载器脚本中还有其他工作正常的东西,所以我会发布整个东西,但我遇到了最底层代码的问题! Thank you!谢谢!

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

public class SceneLoader : MonoBehaviour
{
  public string scenename;
  public GameManager GM;
 

 private void OnTriggerEnter2D(Collider2D collision)
  {
    if(collision.tag == "Player")
    {
      SceneManager.LoadScene(scenename);
    }
  }


    private void Deathscene()
    {
      if(GM.LifeTotal == 0)
        {
           SceneManager.LoadScene(Bob); 
        }
    }

}

Gamemanager script 

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

public class GameManager : MonoBehaviour
{
public int PotionsCollected = 0;
public int LifeTotal = 3;

    public Text PotionsOutput;
    public Text LifeOutput;
    void Update()
    {
        PotionsOutput.text = "Potions: " + PotionsCollected;
          LifeOutput.text = "Life: " + LifeTotal;
    }

    public void CollectPotion()
    {
        PotionsCollected++;
    }
    public void UsePotion()
    {
        PotionsCollected--;
    }
    public void LoseLife()
    {
        LifeTotal--;
    }
}

What you can do is from your Unity Editor go to File->Build Settings and then drag and drop inside the active scenes window your death scene.您可以做的是从您的 Unity 编辑器转到 File->Build Settings,然后将您的死亡场景拖放到活动场景窗口中。

Then an index will be generated on the right side of the window and you can use that index to load the scene.然后会在窗口右侧生成一个索引,您可以使用该索引加载场景。 like this:像这样:

SceneManager.LoadScene("Use your generated index");

NOTE: Use your index as a number and not as a string.注意:将索引用作数字而不是字符串。

UPDATED Solution:更新的解决方案:

public void LoseLife() 
{
    LifeTotal--; 

    if(LifeTotal <= 0)
    {
         SceneManager.LoadScene("Use your generated index");
    }
}

I supposse LoseLife() it's called when the enemy attacks your player.我想LoseLife()当敌人攻击你的玩家时它会被调用。

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

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