Я занимаюсь созданием 2D-игры «Разрушитель блоков» и пытаюсь создать планку здоровья (с 3 звездами).Жизнь пользователя должна уменьшаться всякий раз, когда мяч попадает в коллайдер, однако, когда мяч попадает в невидимый коллайдер, он автоматически попадает на сцену «Потерять».Я пытаюсь вызвать переменную «здоровье» из другого сценария, но она не работает.Я знаю, что делаю что-то не так, но не могу понять, что это такое.Любые предложения?
На изображении ниже представлен инспектор 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;
}
}
}
Скрипт LoadScene:
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");
}
}