Search Unity

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

Discussion in 'Immediate Mode GUI (IMGUI)' started by Purple-Night-Owl, Apr 26, 2016.

  1. Purple-Night-Owl

    Purple-Night-Owl

    Joined:
    Oct 30, 2015
    Posts:
    83
    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.
     
  2. asim314

    asim314

    Joined:
    May 7, 2014
    Posts:
    14
    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<Text> (); //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.
     
    Purple-Night-Owl likes this.