How do I add collected items to GUI canvas and text?

I’m not sure if this is the right place to post this question but I think it’s appropriate, but it’s about scripting too. I want to use Unity 5’s GUI system for my game to display a count for items that the player collects in a level. So for example, when the game starts it’s 0/4 (4 being the total items to collect), and then for every item collected a point is added to the counter so it becomes 1/4 and so on until all items are collected. I managed to crack this using a scripted UI using OnGUI, but now I want to use something more sophisticated so I added a canvas with some HUD I created myself and text. I’d like to script the text to work as I explained before, but I’m not sure how to do it with Canvas and text. Do I need to call the Canvas component? I’ve tried looking online but all I can find so far are answers to OnGUI. Sorry to bother anyone with this. I only usually come here as a last resort. If I find a way to do it, I’ll post it here.

Well if i am not wrong you wanted to know about displaying your text using unity UI. For this just add
using UnityEngine.UI;
Text mytext; // and then declare a Text Variable inside your class.

mytext = GetComponent (); //then get the Text Component

mytext.text = "Counter: " + Counter; // Simply assign your counter to the Text Component.

You can attach this script to your UI Text component in the inspector or you can make the Text as public and then you can drag and drop the text component you want to use.

1 Like