I have a game over canvas that’s called GameOverUI and it is GameOverUI.SetActive(false); in Start(). There is a TextMeshProUGUI element that I have on that Canvas that i’m trying to access from my PlayerHealth script (attached to main camera) that will change its .text attribute according to who activates the GameOverUI. I can attach it to the inspector spot when the game isn’t playing, however, when i press play, the TextMeshProUGUI turns into “none”. While it is playing, if i pause the game and drag the TextMeshProUGUI text onto the empty “none” space, the code works perfectly. It updates the text and displays it when a player dies. How can I keep it from dumping the reference on play? When i press the play button again (stop) the text reattaches itself to the script. I’m sure it’s something stupid. Here’s the PlayerHealth Script.
//Attach to Main Camera, Health Manager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class PlayerHealth : MonoBehaviour
{
//Variables for Health/Damage Tracking
private float maxHealth = 100f;
private float minHealth = 1f;
public float player1Health = 100f;
public float player2Health = 100f;
private int playerNumber;
private int damage;
[SerializeField]
private TextMeshProUGUI gameOverText;
[SerializeField]
private Image player1HealthBar;
[SerializeField]
private Image player2HealthBar;
[SerializeField]
private GameObject GameOverUI;
public void Start()
{
GameOverUI.SetActive(false);
gameOverText = GetComponent();
}
public void Update()
{
player1HealthBar.fillAmount = player1Health / maxHealth;
player2HealthBar.fillAmount = player2Health / maxHealth;
//Keep from overhealing
if (player1Health > maxHealth)
{
player1Health = maxHealth;
}
if (player2Health > maxHealth)
{
player2Health = maxHealth;
}
//Check for Game Over and Display Game over dialog
if(player1Health < minHealth)
{
GameOverUI.SetActive(true);
Time.timeScale = 0;
gameOverText.text = “Player 2 Wins”;
}
if(player2Health < minHealth)
{
GameOverUI.SetActive(true);
Time.timeScale = 0;
gameOverText.text = “Player 1 Wins”;
}
}
public void HealthManager(int pNum, int dmg)
{
playerNumber = pNum;
damage = dmg;
Debug.Log(“HealthManagerGettingCalled”+playerNumber+damage);
if (playerNumber == 1)
{
player1Health -= damage;
Debug.Log(“Player " + playerNumber +” was hit");
}
if(playerNumber == 2)
{
player2Health -= damage;
Debug.Log(“Player " + playerNumber + " was hit”);
}
}
}