Search Unity

Display dynamic text in multiple positions

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by DTsoukalos, Jul 11, 2019.

  1. DTsoukalos

    DTsoukalos

    Joined:
    Jul 11, 2019
    Posts:
    2
    Hi.
    I am beginner in unity so i suppose this is a easy question but i have stack for some days and i would greatly appreciate if someone could help me.
    I want to display a dynamic text to multiple positions without making a serialized field for each text.
    For example i make a button that increases a number and it displays on a text every time i press it.
    I want to display it in 2 or more texts.
    One idea that i had was to make a prefab text. So i make a prefab and I drop it at the serialized field, then i put it at the canvas and i press play and then something strange happens!
    When i press the button to increase the text nothing happens but when i press the play again to stop the scene the text changes it immediately to the sum of numbers that i press the button!

    What am i doing wrong and is there a better way to get it done ?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class test : MonoBehaviour
    7. {
    8.     public int okk=1;
    9.     [SerializeField] Text ok;
    10.  
    11.     void Update()
    12.     {
    13.         ok.text = okk.ToString();
    14.     }
    15.  
    16.     public void click()
    17.     {
    18.         okk += 1;
    19.     }
    20. }
    21.  
    Screenshot_2.png

    Screenshot_3.png
    Screenshot_4.png

    Thank you!