- Home /
Display actual number of bullets
This should be an easy one- While using the new GUI, how would I display the exact number of bullets/torpedos, etc in a slider? Currently, it works, but there are too many to start out with.
Why a slider? what wortks currently? There are too many of what to start out with? Please be more clear.
$$anonymous$$y apologies. I used a slider for the battery, which works quite well, so I thought I would try a slider. Would you recommend something else? In this case, I want to show how many torpedos I have. If I start with 4, I want to display just four. Right now, there are many more showing up than the variable number tells. Does that help?
Answer by Cherno · Jul 13, 2015 at 08:39 PM
Hold on a second. First you talked about bullets, then battery, and now torpedos? Just call it ammo, it's less confusing :)
using UnityEngine.UI;
public class PlayerGUI : MonoBehaviour {
public Text text_ammo;
public int ammo;
public int ammo_max;
public void UpdateAmmoText() {
text_ammo.text = ammo.ToString() + "/" + ammo_max.ToString();
}
}
You have to drag you Text UI Object into the text_ammo slot in the inspector. Set the ammo and ammo_max however you want, and call the UpdateAmmoText option whenever one of the values change (Firing, reloading).
A slider is possible as well of course, check the Scripting API for the relevant variables.
Your answer
Follow this Question
Related Questions
Set Slider value but don't trigger On Value Changed 4 Answers
OnGUI sliders only accept float? C# 2 Answers
Horizontal slider won't work 1 Answer
Distribute terrain in zones 3 Answers
Horizontal Slider with Labels 3 Answers