简体   繁体   English

如何修复健康栏

[英]How to fix the health bar

I am creating A block-breaker 2D game and I am trying to do a health bar (with 3 stars).我正在创建一个块破坏 2D 游戏,我正在尝试做一个健康酒吧(3 星)。 The user's lives are supposed to decrease whenever the ball hits the collider, however, when the ball hits the invisible collider it automatically goes to the "Lose" scene.每当球击中碰撞器时,用户的生命应该会减少,但是,当球击中隐形碰撞器时,它会自动进入“失败”场景。 I am trying to call the variable 'health' from another script but it is not working.我试图从另一个脚本调用变量“health”,但它不起作用。 I know I am doing something wrong but I cannot seem to find out what it is.我知道我做错了什么,但我似乎无法找出它是什么。 Any suggestions?有什么建议?

The below image is the inspector of LivesStars1, LivesStars2, LivesStars3下图是 LivesStars1、LivesStars2、LivesStars3 的检查员LivesStars1、LivesStars2、LivesStars3 的检查员

Health Script :健康脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Health : MonoBehaviour {


public GameObject LivesStars1, LivesStars2, LivesStars3;
public static int health;

void Start()
{
    health = 3;
    LivesStars1.gameObject.SetActive(true); //The object LiveStars1 is being enabled
    LivesStars2.gameObject.SetActive(true); //The object LiveStars2 is being enabled
    LivesStars3.gameObject.SetActive(true); //The object LiveStars3 is being enabled

}

void Update()
{
    int health = health;

    switch (health)
    {
        case 3: //If the user doesn't lose a life all 3 lives remain
            LivesStars1.gameObject.SetActive(true); //The object LiveStars1 is being enabled
            LivesStars2.gameObject.SetActive(true); //The object LiveStars2 is being enabled
            LivesStars3.gameObject.SetActive(true); //The object LiveStars3 is being enabled
            break;
        case 2: //If the user loses one life only LivesStars3 is disabled
            LivesStars1.gameObject.SetActive(true);
            LivesStars2.gameObject.SetActive(true);
            LivesStars3.gameObject.SetActive(false);
            break;
        case 1: //If the user loses two lives then LivesStars2 will also be disabled
            LivesStars1.gameObject.SetActive(true);
            LivesStars2.gameObject.SetActive(false);
            LivesStars3.gameObject.SetActive(false);
            break;
        case 0: //If the uses loses all his lives then the Lose scene is enabled
            LivesStars1.gameObject.SetActive(false);
            LivesStars2.gameObject.SetActive(false);
            LivesStars3.gameObject.SetActive(false);
            break;

    }
  }
}

LoadScene Script:加载场景脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoadScenes : MonoBehaviour {

public LevelManager lvlManager;

//If the ball hits one of the walls the colliders will be triggered
void OnTriggerEnter2D()
{
    print("The wall is triggered by the ball");
    lvlManager.LoadLevel("Lose");
    Bricks.brickCount = 0;

    if(Health.health == 0)
    {
       lvlManager.LoadLevel("Lose");
    }
}



void OnCollisionEnter2D()
{
    Debug.Log("The ball has collided with the wall");
 }

}

It's not the solution you're looking for, but what I see is that you are constantly repeating yourself in the code. 这不是您正在寻找的解决方案,但我看到的是您不断重复自己的代码。

Change the switch to this: If the code seems magic to you. switch更改为:如果代码看起来很神奇。 Message me 给我发短信

switch (health)
    {
        case 0: //If the uses loses all his lives then the Lose scene is enabled
            SetLivesStarsCondition(true, true, true);
            break;

        case 1: //If the user loses two lives then LivesStars2 will also be disabled
            SetLivesStarsCondition(true, true, false);
            break;

        case 2: //If the user loses one life only LivesStars3 is disabled
            SetLivesStarsCondition(true, false, false);
            break;

        case 3: //If the user doesn't lose a life all 3 lives remain
            SetLivesStarsCondition(false, false, false);
            break;
    }

private void SetLivesStarsCondition(bool starOne, bool starTwo, bool starThree)
{
    LivesStars1.gameObject.SetActive(starOne);
    LivesStars2.gameObject.SetActive(startwo);
    LivesStars3.gameObject.SetActive(starThree);
}

The problem seems to be in the "LoadScenes" script When something enters a trigger OnTriggerEnter2D is called. 问题似乎是在“LoadScenes”脚本中当某些东西进入触发器时OnTriggerEnter2D被调用。 Before you are checking if your health is 0 you are already switching to your "Lose" Level. 在检查您的健康状况是否为0之前,您已经切换到“失去”等级。

This is your original code ( I added an comment to the line that causes this behaviour ) 这是您的原始代码(我在导致此行为的行中添加了注释)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoadScenes : MonoBehaviour {

public LevelManager lvlManager;

//If the ball hits one of the walls the colliders will be triggered
void OnTriggerEnter2D()
{
    print("The wall is triggered by the ball");
    lvlManager.LoadLevel("Lose"); // <-- This here is your problem
    Bricks.brickCount = 0;

    if(Health.health == 0)
    {
       lvlManager.LoadLevel("Lose");
    }
}



void OnCollisionEnter2D()
{
    Debug.Log("The ball has collided with the wall");
 }

}

And this is what is should be: 这应该是:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoadScenes : MonoBehaviour {

public LevelManager lvlManager;

//If the ball hits one of the walls the colliders will be triggered
void OnTriggerEnter2D()
{
    print("The wall is triggered by the ball");

    Bricks.brickCount = 0;

    if(Health.health == 0)
    {
       lvlManager.LoadLevel("Lose");
    }
}



void OnCollisionEnter2D()
{
    Debug.Log("The ball has collided with the wall");
 }

}

TLDR: You have an extra and unnecessary lvlManager.LoadLevel("Lose"); TLDR:你有一个额外的和不必要的lvlManager.LoadLevel("Lose"); before you checked the player health. 在检查玩家健康状况之前。

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

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