Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

TextMeshProUGUI detaching from PlayerHealth script on Play

Discussion in 'UGUI & TextMesh Pro' started by anthonymiller1988, May 1, 2018.

  1. anthonymiller1988

    anthonymiller1988

    Joined:
    Feb 23, 2018
    Posts:
    2
    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<TextMeshProUGUI>();
    }
    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");
    }
    }
    }
     

    Attached Files:

  2. anthonymiller1988

    anthonymiller1988

    Joined:
    Feb 23, 2018
    Posts:
    2
    Solved, i attached a script exclusively to the PlayerWin TextMeshProUGUI object that pulled a playerWon variable from the HealthManager and updated it's own .text in it's own Update(). Works like a charm. Any ?? just holler.
     
  3. kryzodoze

    kryzodoze

    Joined:
    Nov 6, 2013
    Posts:
    21
    This stub:
    Code (CSharp):
    1. gameOverText = GetComponent<TextMeshProUGUI>();
    isn't doing what you think it is.

    What that does is looks on the current GameObject and tries to find that component. The reason that your field is getting overwritten is because you try to do that in your Start() routine, so it overwrites anything that was in it.

    My recommendation is just to remove that line from Start(), and drag in your reference while in edit mode, then it should stay there when you play.