- Home /
How do I get Unity to report "You have 1 bullet remaining" instead of "You have 1 bullets remaining"?
Hello So I just started doing some coding on Unity (yes, I'm a complete noob). My codes looks like this:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ConsoleTest : MonoBehaviour { private int ammo = 5; // Start is called before the first frame update void Start() { //Debug.Log("Inside the Start function"); Debug.Log("Game has started. Reading ammo as" + ammo); //ammo = ammo - 1; //Debug.Log("Now reading ammo as" + ammo); } // Update is called once per frame void Update() { //Debug.Log("Inside the Update function"); if (Input.GetMouseButtonDown(0)) { //Debug.Log("Reading LMB clicked"); if (ammo > 0) { ammo = ammo - 1; Debug.Log("You have" + ammo + "bullets remaining"); }
else if (ammo < 1) { Debug.Log("Your gun is empty. Hit R to reload"); }
} } } Whenever I click my LMB, the amount of ammo will be decreased and reported to me through the console screen of Unity. But what I'm struggling with is I when my ammo was decreased to 1, I want Unity to report "I have 1 bullet remaining" instead of "You have 1 bullets remaining". I tried changing if to if (ammo > 0 && ammo != 1) as well as adding else if (ammo == 1) but none have worked. So if there's someone can help, I would be very appreciated. Thank you so much.
Answer by jmhoubre · Nov 12, 2021 at 10:45 PM
private void CheckAmmo (int ammo)
{
Debug.Log ($"You have {ammo} bullet{(ammo < 2 ? "" : "s")} remaining.");
}
Answer by ariesight · Nov 12, 2021 at 09:55 AM
Try this:
void Update() {
if (Input.GetMouseButtonDown(0)) {
if (ammo > 1) {
ammo = ammo - 1;
if (ammo == 1)
Debug.Log("You have 1 bullet remaining");
else
Debug.Log("You have" + ammo + "bullets remaining");
}
else if (ammo < 1) { // You can remove the extra check here, btw - a simple else is enough
Debug.Log("Your gun is empty. Hit R to reload");
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How would I go about create multiple cursors controlled by multiple players 1 Answer
Distribute terrain in zones 3 Answers
Making Everything Public 0 Answers
Pause System not working :/ 2 Answers