Display the laser fire count in Unity UI

Samarth Dhroov
1 min readJun 6, 2021

--

The exercise here demonstrates changing the text of the Text element in a UI placed in the Unity game.

Before going further into this, please go through the article here.

The process is extremely simple and shall not take more than 5 minutes to implement.

  • Since the fire count variable is already set, it could be used to change the text upon the condition of empty ammo as well as real-time ammo count by passing it to the text.
  • The count variable is an integer but the text element needs a string and hence, we need to convert the int to a string using the ToString( ) method.
  • Concatenate to have a denominator displayed with the live count.
if (_totalLaserFired < 15)
{
_totalLaserFired++;
_noAmmoText.GetComponent<Text>().text = _totalLaserFired.ToString() + "/15"; // Updates text to current count of fired lasers.
  • Now, add the else part that swaps the text from ammo count to “No Ammo”.
else
{
_noAmmoText.GetComponent<Text>().text = "No Ammo"; // Changes the text to No Ammo.
}

That should be it.

Here is the final result :)

Thank you very much

--

--

No responses yet