[英]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.