Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

I'm kind of struggling with this

Discussion in 'Getting Started' started by DungDo27, Nov 12, 2021.

  1. DungDo27

    DungDo27

    Joined:
    Sep 28, 2021
    Posts:
    13
    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". If there's someone can help, I would be very appreciated. Thank you so much
     

    Attached Files:

  2. larsp777

    larsp777

    Joined:
    Nov 10, 2021
    Posts:
    5
    Is it the text you want to change?
     
  3. DungDo27

    DungDo27

    Joined:
    Sep 28, 2021
    Posts:
    13
    Kind of I think. It goes like this, the amount of bullets starts at 5, after every click that amount of bullets will be reported to me (You have 4 bullets remaining, you have 3 bullets remaining, etc). So when the amount of bullets reach 1, I want it to report "You have 1 bullet remaining" rather than "You have 1 bullets remaining".
     
  4. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    481
    Code (CSharp):
    1. //Debug.Log("Inside the Update function");
    2.         if (Input.GetMouseButtonDown(0))
    3.         {
    4.             //Debug.Log("Reading LMB clicked");
    5.  
    6.             //Condiiton 1
    7.             if (ammo > 2)
    8.             {
    9.                 ammo = ammo - 1; // ammo = 4 then 3 then 2
    10.                 Debug.Log("You have" + ammo + " bullets remaining");
    11.             }
    12.  
    13.             //Condition 1 is no longer true so on to Condition 2
    14.  
    15.             //Condition 2
    16.             else if (ammo == 2) // this condition is now true
    17.             {
    18.                 ammo = ammo - 1; // ammo = 1
    19.                 Debug.Log("You have" + ammo + " bullet remaining");
    20.                 ammo = ammo - 1; // ammo = 0
    21.             }
    22.  
    23.             //Condiition 2 is no longer true so on to Condition 3
    24.  
    25.             //Condition 3
    26.             else if (ammo < 1) // this condition is now true
    27.             {
    28.                 Debug.Log("Your gun is empty. Hit R to reload");
    29.             }
    30.  
    31.         }
     
    DungDo27 likes this.
  5. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    Code (CSharp):
    1. string message = String.Format("You have {0} bullet{1} remaining.", ammo,
    2.                 ammo == 1 ? "" : "s");
    3. Debug.Log(message);
     
  6. DungDo27

    DungDo27

    Joined:
    Sep 28, 2021
    Posts:
    13
    Thank you so much for helping! It worked really well