【发布时间】:2019-04-29 05:10:15
【问题描述】:
我正在创建一个破坏方块的 2D 游戏,并且我正在尝试制作一个健康栏(3 颗星)。每当球击中对撞机时,用户的生命应该会减少,但是,当球击中不可见的对撞机时,它会自动进入“Lose”场景。我试图从另一个脚本中调用变量“健康”,但它不起作用。我知道我做错了什么,但我似乎无法找出它是什么。有什么建议吗?
下图是 LivesStars1、LivesStars2、LivesStars3 的检查器
健康脚本:
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;
}
}
}
加载场景脚本:
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");
}
}
【问题讨论】:
-
lvlManager.LoadLevel("Lose");这正是你所做的。 -
什么意思?