简体   繁体   English

我正在尝试在 Unity 中通过场景运行游戏

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

I am Korean and I wrote it using Google Translator, and the content may not be delivered properly.我是韩国人,我用谷歌翻译写的,内容可能无法正确传递。

Hello.你好。 I'm asking for help because I couldn't solve my school homework.我正在寻求帮助,因为我无法完成我的学校作业。 Unity wants to implement the game-over function when a player dies. Unity 希望在玩家死亡时实现游戏结束功能。 It's not working as well as I thought.它没有我想象的那么好。 I'd like to hear advice from a master.我想听听大师的建议。

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

You are doing a lot of extra work here, and you have some stuff that isn't being used at all.你在这里做了很多额外的工作,你有一些根本没有被使用的东西。 You never use the three GameObjects life1, life2, and life3.你永远不会使用三个游戏对象 life1、life2 和 life3。

Simply put, your errors come from the fact that 'life' is never defined within the code you've shown here.简而言之,您的错误来自这样一个事实,即您在这里显示的代码中从未定义过“生活”。

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

Your first error is here, you are checking if 'life' is <= 0, but 'life' doesn't exist.您的第一个错误在这里,您正在检查 'life' 是否 <= 0,但 'life' 不存在。

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

Instead you should use lifeCount, which you've already defined above.相反,您应该使用 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

Your second error is found here:您的第二个错误在这里发现:

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

    }
}

I think you are trying to reference the Life class here.我认为您正在尝试在这里引用 Life 课程。 Aside from the issue of this being a non-static method, you've again written life lowercase, which does not exist.除了这是一种非静态方法的问题之外,您再次编写了不存在的life小写字母。 Also you've written IsDead() as lsDead, with the lowercase letter 'L' instead of an uppercase letter "I".此外,您已将 IsDead() 编写为 lsDead,用小写字母“L”代替大写字母“I”。 And you have no () to reference the method.而且您没有 () 来引用该方法。

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

    }
}

In the future I would recommend properly formatting your question as well, as it can be really hard to read when there are unwritten code snippets all about.将来,我也建议您正确格式化您的问题,因为当存在未编写的代码片段时,它真的很难阅读。 You can select a block of code and press 'CTRL + K' to automatically format it as a code block.您可以选择一个代码块并按“CTRL + K”自动将其格式化为代码块。

暂无
暂无

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

相关问题 如何将我的分数游戏 Object 从我的游戏场景转移到 Unity 中的游戏结束场景 - How can I Transfer my Score Game Object from my game scene to game over scene in Unity Game Over Scene不起作用(Unity C#) - Game Over Scene doesn't work (Unity C#) 在Block Breaker游戏中(Unity3D)我试图在所有积木都毁掉时加载关卡 - (Unity3D) in Block Breaker game I'm trying to load levels when all bricks destroyed 重新加载游戏场景时,对象不会统一加载 - When I reload my game scene the objects do not reload in unity 仅当我将鼠标悬停在场景或游戏视图上时,小控件才会更新 - Gizmos only update when I hover over the scene or game view 如果我在学分场景之前进入游戏场景,使面板向上滚动的Unity 2018脚本将无法工作 - Unity 2018 script to make panel scroll up wont work if I go to the game scene before the credits scene 在Unity中输入多维数据集时尝试加载新场景 - Trying to load a new scene when I enter a cube in Unity 我正在努力使玩游戏的人可以在触发器中按他们想要的次数按“E” | Unity 3d + 动画师 - I'm trying to make it so the person playing the game can press "E" as much as they want in the trigger | Unity 3d + animator 我正在统一制作 FPS 游戏并不断收到 1 个错误 - I'm making an FPS game in unity and keep getting 1 error 我正在统一制作 FPS 游戏并不断收到这些错误 - I'm making an FPS game in unity and keep getting these errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM