There is a Movie script which serves to control the player and it contains the number of health. It is necessary to display the number of HP on the Canvas in the Text. I do this through the Game script.

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Game : MonoBehaviour { public Text Zdor; public Text Level; public Text Point; public string mytext; // Update is called once per frame void Update() { Movie hp = gameObject.GetComponent("health") as Movie; Zdor = hp; } } 

    2 answers 2

    In a hurry script, he is looking for a player and a script in it. Throws himself on the Text, which will display:

     using UnityEngine; using UnityEngine.UI; using System.Collections; public class HealthScript : MonoBehaviour { public Movie HP; // Use this for initialization void Start () { HP = GameObject.Find("Player").GetComponent<Movie>(); } // Update is called once per frame void Update () { gameObject.GetComponent<Text>().text = HP.health.ToString(); } } 

    I checked it on my own - it displays 100 on the screen instead of the standard New Text. If you need a script on the canvas, then this Text Zdor as well as the player to find, then pass him the text:

     GameObject.Find("Zdor").GetComponent<Text>().text = HP.health.ToString(); 

    also with Level and Point.

      The Movie class is not specified, so something like this:

       public class Movie : MonoBehaviour { public int hp; .... } public class Game : MonoBehaviour { public Text Zdor; public Text Level; public Text Point; public string mytext; Movie movie; void Start(){ // Не нужно получать компонент в Update, каждый кадр - сделайте это один раз в Start или Awake. movie = gameObject.GetComponent("health") as Movie; } // Update is called once per frame void Update() { Zdor.text = movie.hp; } } 

      Of course, if everything is correctly initialized.

      For reading:

      https://docs.unity3d.com/ScriptReference/UI.Text-text.html

      https://docs.microsoft.com/ru-ru/dotnet/csharp/programming-guide/types/#custom-types

      https://docs.microsoft.com/ru-ru/dotnet/csharp/language-reference/keywords/reference-types

      • I thought to write to the update because the number of hp will change, or is it not important? - Igor Brusko
      • Movie is a reference data type, so this is not important. supplemented the answer. - Valera Kvip