Search Unity

Creating Score with new UI Text

Discussion in 'UGUI & TextMesh Pro' started by fabianampires, Sep 3, 2014.

  1. fabianampires

    fabianampires

    Joined:
    Mar 30, 2014
    Posts:
    12
    Hi Everyone,

    I'm new to Unity, but not a total newbie (have already created games with it). I'm having a really hard time adapting to the new Unity UI. I've been trying to add a score to my scene and can't get it to work (update at run time), because no matter how hard I try to reference it to the code, it just doesn't work!

    Here's what I'm doing:
    - I have created a UIText object in Unity
    - I have a scoring.cs code that increments my score (I've declared "using unityengine.UI")
    - In scoring.cs I created a public Text object called score
    - I change that Text object at runtime: score.text="Score: " + iscore (where iscore is an int variable that I increment over time)
    - In Unity, I add scoring.cs to my UIText

    Then I'm trying to associate my UI text from the editor to my public score. I've tried it all, can't make it happen :(

    I followed the instructions given to two similar threads in this forum, none worked. So I'm creating this new thread to see if someone can point me to the right direction or to an online tutorial I can follow. (The current official ones don't go into details like that).

    Thanks,
    Fabiana
     
    Last edited: Sep 3, 2014
    raajcm1 likes this.
  2. DoubleNibble

    DoubleNibble

    Joined:
    Nov 15, 2012
    Posts:
    22
    I just recently revamped scoring system in one of my games from 4.5 to 4.6. Maybe I can help. Can you show your complete scoring.cs file here?
     
  3. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,037
    The steps should be simple:
    • Add an object from UI->Text
    • Make a public Text variable in a component
    • Drag the Text object to the slot in the inspector
    It seems like you're already doing this, and I can't think of any reason it shouldn't work :)
     
  4. fabianampires

    fabianampires

    Joined:
    Mar 30, 2014
    Posts:
    12
    Thank you Orb and Malapropos,

    It turns out that my scoring.cs code was right.

    The part that is not working is the broadcast call I'm making from another file to my update_score function in my scoring.cs.

    Here's the code on scoring.cs

    usingUnityEngine;
    usingSystem.Collections;
    usingUnityEngine.UI;

    public class Scoring : MonoBehaviour {


    public Text score;
    private int score_count;


    public void update_score(){

    score_count +=1;
    score.text="Pontos: " + score_count;

    }
    }


    And I'm trying to call update_score from another script (it's a long one, so i'm pasting just the actual call here):

    private GameObject score;

    void Update()
    {
    score = (GameObject) GameObject.FindWithTag ("score");
    score.BroadcastMessage("update_score");
    }


    I think the way to fix it is to use Text instead of GameObject. I tried that but then I wasn't able to use .FindWithTag with a Text object.. Any ideas on how to make this work?
     
    raajcm1 likes this.
  5. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    why not just do:

    Code (csharp):
    1.  
    2. void Update()
    3. {
    4.     var scoreGO = (GameObject) GameObject.FindWithTag ("score");
    5.     var scoreComp = scoreGO.GetComponent<Scoring>();
    6.     scoreComp.update_score();
    7. }
    8.  

    This avoids doing a message broadcast and is a bit more rigorous.
     
    fabianampires likes this.
  6. DoubleNibble

    DoubleNibble

    Joined:
    Nov 15, 2012
    Posts:
    22
    Here is yet another alternative way that worked for me using events:

    Code (CSharp):
    1. public class ScoreCounter : MonoBehaviour
    2. {
    3.     private Text _text;
    4.  
    5.     void Start()
    6.     {
    7.         GameManager.OnScoreUpdated += UpdateScore;
    8.         _text = GetComponent<Text>();
    9.         if (_text != null)
    10.             _text.text = "SCORE\n" + GameManager.Instance.Score;
    11.     }
    12.  
    13.     private void UpdateScore()
    14.     {
    15.         if (_text != null)
    16.             _text.text = "SCORE\n" + GameManager.Instance.Score;
    17.     }
    18.  
    19.     void OnDestroy()
    20.     {
    21.         GameManager.OnScoreUpdated -= UpdateScore;
    22.     }
    23. }
    Code (CSharp):
    1. public class GameManager : Manager<GameManager>
    2. {
    3.     public int Score;
    4.     public int ScoreMultiplier;
    5.  
    6.     public delegate void ScoreUpdatedAction();
    7.     public static event ScoreUpdatedAction OnScoreUpdated;
    8.  
    9.     private void PickedUpFish()
    10.     {
    11.         if (OnScoreUpdated != null)
    12.             OnScoreUpdated();
    13.     }
    14. }
    Player is meant to collect fishes in the game and PickedUpFish() is fired up every time that happens. Manager (class) is a generic singleton class for managers I use (audio, game state, etc.). This has the advantage of only being called when needed instead of every update.

    TIm C's code is admittedly more simple and should work with your existing code easier. I'm using this, because I have other objects which need to know when score is updated and I can just subscribe them to this event too.
     
    Last edited: Sep 4, 2014
    fabianampires likes this.
  7. Wyckthor

    Wyckthor

    Joined:
    Jul 9, 2014
    Posts:
    2
    Hello Fabiana!

    I have the answer for your last question: "...I wasn't able to use .FindWithTag with a Text object.. Any ideas on how to make this work?"

    Simply write this way:
    Text something = (GameObject.FindWithTag("AnyTag")).GetComponent<Text>();
     
    fabianampires likes this.
  8. fabianampires

    fabianampires

    Joined:
    Mar 30, 2014
    Posts:
    12
    Hi Everyone,

    Thank you for all your replies. Tim and Wyckthor, I've used both of your solutions and now my score is working perfectly!

    Malapropos, your code is so much more organized than mine! I'll definitely save it as reference.
     
    raajcm1 likes this.
  9. GregMeach

    GregMeach

    Joined:
    Dec 5, 2012
    Posts:
    249
    The new "Nightmare" game videos in the learn/Unity 4.6 section are great and show you a lot, well woth the investment of time