繁体   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