如何修复健康栏

时间:2019-04-27 22:13:04

标签: c# unity3d

我正在创建一款突破性的2D游戏,并且我试图做一个健康酒吧(拥有3星)。只要球碰到对撞机,用户的生命就会减少,但是,当球碰到不可见的对撞机时,它会自动进入“丢失”场景。我正在尝试从另一个脚本中调用变量“健康”,但它不起作用。我知道自己做错了,但似乎无法找出问题所在。有什么建议吗?

下图是LivesStars1,LivesStars2,LivesStars3的检查员 The Inspector of LivesStars1, LivesStars2, LivesStars3

运行状况脚本:

<ion-content padding>
    <ion-card *ngFor="let food of shop;let i = index">
        <ion-card-header>
      <ion-title>
        {{i+1}}. {{food.name.first}} {{food.name.last}}
      </ion-title>
    </ion-card-header>
    <ion-toolbar>
      <ion-title>Total Price &nbsp;&nbsp;
        {{food.qty}} Units x ${{food.price}} = 
        ${{food.qty*food.price}}
      </ion-title>
    </ion-toolbar>
        <ion-item>
            <ion-icon name="add-circle" (click)="incrementQty(i)" item-right></ion-icon>
            <ion-input type="number" min="1" [value]="food.qty" [(ngModel)]="food.qty"></ion-input>
            <ion-icon name="remove-circle" (click)="decrementQty(i)" item-right></ion-icon>
        </ion-item>
    </ion-card>
</ion-content>

LoadScene脚本:

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;

    }
  }
}

2 个答案:

答案 0 :(得分:0)

这不是您要寻找的解决方案,但我看到的是您在代码中不断重复自己。

switch更改为此: 如果代码对您来说似乎很神奇。给我发消息

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);
}

答案 1 :(得分:0)

问题似乎出在“ LoadScenes”脚本中 当某些东西进入触发器时,将调用OnTriggerEnter2D。 在检查健康状况是否为0之前,您已经切换到“丢失”等级。

这是您的原始代码(我在引起这种行为的行中添加了注释)

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");
 }

}

这应该是:

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: 在检查播放器运行状况之前,您有多余且不必要的lvlManager.LoadLevel("Lose");