簡體   English   中英

我正在嘗試在 Unity 中通過場景運行游戲

[英]I'm trying to run a game over scene in Unity

我是韓國人,我用谷歌翻譯寫的,內容可能無法正確傳遞。

你好。 我正在尋求幫助,因為我無法完成我的學校作業。 Unity 希望在玩家死亡時實現游戲結束功能。 它沒有我想象的那么好。 我想聽聽大師的建議。

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

public class Life : MonoBehaviour
{
    public int lifeCount = 3;

    public GameObject life1;
    public GameObject life2;
    public GameObject life3;


    public Text lifeText;

    void Update()
    {
        lifeText.text = lifeCount.ToString();
        Debug.Log("number of lives: " + lifeCount);
    }

    public void LifeDecrease()
    {
        lifeCount--;
    }

    private bool IsDead()
    {
        if (life <= 0)
            return true;
        else
            return false;
    }

    public void NoLife()
    {
        if (life.lsDead)
        {
            SceneManager.LoadScene("GameOver");

        }
    }
    }
    

圖片

Assets\Scripts\Logic\Life.cs(38,13): error CS0103: The name 'life' does not exist in the current context
Assets\Scripts\Logic\Life.cs(46,13): error CS0103: The name 'life' does not exist in the current context

你在這里做了很多額外的工作,你有一些根本沒有被使用的東西。 你永遠不會使用三個游戲對象 life1、life2 和 life3。

簡而言之,您的錯誤來自這樣一個事實,即您在這里顯示的代碼中從未定義過“生活”。

Assets\\Scripts\\Logic\\Life.cs(38,13): error CS0103: The name 'life' does not exist in the current context

您的第一個錯誤在這里,您正在檢查 'life' 是否 <= 0,但 'life' 不存在。

private bool IsDead()
{
    if (life <= 0)
        return true;
    else
        return false;
}

相反,您應該使用 lifeCount,您已經在上面定義了它。

private bool IsDead()
{
    if (lifeCount <= 0)
        return true;
    else
        return false;
}

Assets\\Scripts\\Logic\\Life.cs(46,13): error CS0103: The name 'life' does not exist in the current context

您的第二個錯誤在這里發現:

public void NoLife()
{
    if (life.lsDead)
    {
        SceneManager.LoadScene("GameOver");

    }
}

我認為您正在嘗試在這里引用 Life 課程。 除了這是一種非靜態方法的問題之外,您再次編寫了不存在的life小寫字母。 此外,您已將 IsDead() 編寫為 lsDead,用小寫字母“L”代替大寫字母“I”。 而且您沒有 () 來引用該方法。

public void NoLife()
{
    if (IsDead())
    {
        SceneManager.LoadScene("GameOver");

    }
}

將來,我也建議您正確格式化您的問題,因為當存在未編寫的代碼片段時,它真的很難閱讀。 您可以選擇一個代碼塊並按“CTRL + K”自動將其格式化為代碼塊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM