Score text and score health problem

Hello there ! :slight_smile:

I have some trouble understanding the score and health part of the the laser defender course.

Here is my unity view :

Below is my ScoreDisplay.cs

(First question, when i use GetGomponent(), nothing happen, but unlike the course i use findobject which is working)

using UnityEngine;
using UnityEngine.UI;

public class ScoreDisplay : MonoBehaviour
{
    Text scoreText;
    GameSession gameSession;

    
    // Start is called before the first frame update
    void Start()
    {
        scoreText = FindObjectOfType<Text>();
        gameSession = FindObjectOfType<GameSession>();
    }

    // Update is called once per frame
    void Update()
    {
        scoreText.text = gameSession.GetScore().ToString();
    }
}

My main trouble is that we are using two text, one for the health and the other for the score. how unity handle the fact that 2 Text object are there and on both displayScore and DisplayHealth, the coruse is using GetComponent.

Mt problem is, when i use findobecjt of type FindObjectOfType(); on both of the script, my game prompt me two score text.

Below is the displayhealth Script

using UnityEngine;
using UnityEngine.UI;

public class HealthDisplay : MonoBehaviour
{
    Text healthText;
    Player player;


    // Start is called before the first frame update
    void Start()
    {
        healthText = FindObjectOfType<Text>();
        player = FindObjectOfType<Player>();
    }

    // Update is called once per frame
    void Update()
    {
        healthText.text = player.GetLife().ToString();
    }
}

Thank you very much.

Josselin

Privacy & Terms