简体   繁体   中英

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. 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.

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.

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

Instead you should use lifeCount, which you've already defined above.

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. Aside from the issue of this being a non-static method, you've again written life lowercase, which does not exist. Also you've written IsDead() as lsDead, with the lowercase letter 'L' instead of an uppercase letter "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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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