Search Unity

Prefab updating UI Text Elements

Discussion in 'UGUI & TextMesh Pro' started by OliverKohl, Aug 2, 2018.

  1. OliverKohl

    OliverKohl

    Joined:
    Jun 6, 2014
    Posts:
    9
    Hi all,

    I have to ask for support. I'm pretty new to Unity, so I do not understand which objects I should make as a prefab and which not.

    I created a UI GameObject consisting of SpriteRenderers, Text Elements and so on. That's my buyable trading good. I got a script on this, which should have a reference to the GameMaster object

    Code (CSharp):
    1.  
    2. public class Warenkauf : MonoBehaviour
    3. {
    4.     public AudioSource kaufen;
    5.     public GameObject spielObjekt;
    6.     private GameMaster spielverwaltung;
    7.  
    8.     void Awake()
    9.     {
    10.         spielverwaltung = spielObjekt.GetComponent<GameMaster>();
    11.         if (spielverwaltung == null)
    12.         {
    13.             Debug.LogError("SV nicht ladbar.");
    14.         }
    15.     }
    16.  
    17.     public void OnMouseDown()
    18.     {
    19.         …
    20.             spielverwaltung.GeldAktualisieren(...);
    21.         ...
    22.     }
    23. }
    The GameMaster class is defined as a Singleton. (Sorry for using German language in Code :))

    Code (CSharp):
    1.        
    2. //GameMaster setup
    3.         void Awake()
    4.         {
    5.             // Setup eines Singleton Patterns in Unity.
    6.             if (instanz == null)
    7.             {
    8.                 instanz = this;
    9.             }
    10.             else if (instanz != this)
    11.             {
    12.                 Destroy(gameObject);
    13.             }
    14.  
    15.             DontDestroyOnLoad(gameObject);
    16.         }
    17.  
    Within the GameMaster object, I would like to declare a method, which updates my UI Elements. So I have 3 public Text variables to do that:

    Code (CSharp):
    1.  
    2.  
    3.  
    4. public void GeldAktualisieren(...)
    5.         {
    6.             // Code never reached!
    7.  
    8.             if (Spiel.spieler == null)
    9.             {
    10.                 return;
    11.             }
    12.  
    13.             gold.text = Spiel.spieler.vermögen.getGold().ToString();
    14.             silber.text = Spiel.spieler.vermögen.getSilber().ToString();
    15.             kupfer.text = Spiel.spieler.vermögen.getKupfer().ToString();
    16.         }
    17.  
    When I make a prefab out of the buyable trading good gameObject and instantiate this, it loses the Connection to the GameMaster object. If I make a prefab out of the GameMaster object, too, it won't update the 3 UI Texts (Gold, Silber, Kupfer) anymore - simply nothing is happened and I do not know why.

    Here are my 3 1/2 Questions:
    1.) What is the correct procedure to handle such Kind of Problems?
    2.) Which objects should be declared as Prefabs and which shouldn't?
    3.) What is the reason in my described case the UI Elements on the screen are not updated? Why is the Code in method GeldAktualisieren() never reached?

    The wrong behaviour only accurs when I instantiate the trading good Elements.
    Thanks for your help and advice,

    Oliver