Search Unity

Why isn't my character firing?

Discussion in 'Scripting' started by jitterywriter, Jan 15, 2022.

  1. jitterywriter

    jitterywriter

    Joined:
    Jan 7, 2022
    Posts:
    2
    I have a simple method that checks to see whether the player has any ammo remaining within my GameSession script:

    Code (CSharp):
    1. [SerializeField] int playerAmmo = 5;
    2. public bool ammoRemaining;
    3.  
    4. private void Start()
    5. {
    6. if (playerAmmo > 0)
    7.         {
    8.             ammoRemaining = true;
    9.         }
    10. }
    11.  
    12.  
    13.     public void PlayerFire(int ammoUsed)
    14.     {
    15.         if (playerAmmo > 0)
    16.         {
    17.             playerAmmo = playerAmmo - ammoUsed;
    18.             ammoText.text = "Arrows: " + playerAmmo.ToString();
    19.             ammoRemaining = true;
    20.         }
    21.         else
    22.         {
    23.             ammoRemaining = false;
    24.         }
    25.     }
    and in the player controller within start I have :
    hasArrows = FindObjectOfType<GameSession>().ammoRemaining;

    and a method that reads the following:


    Code (CSharp):
    1. void OnFire(InputValue value)
    2.     {
    3.         if (!isAlive)
    4.         {
    5.             return;
    6.         }
    7.         if(!hasArrows)
    8.         {
    9.             return;
    10.         }
    11. FindObjectOfType<GameSession>().PlayerFire(1);
    12.             Instantiate(arrow, arrowSpawn.position, transform.rotation);
    13.             playerAnimator.SetTrigger("Shoot");
    Where have I gone wrong? I can confirm the public bool IS reading as true when the game loads but the player script doesn't seem to care...
     
  2. jitterywriter

    jitterywriter

    Joined:
    Jan 7, 2022
    Posts:
    2
    RIGHT figured it out.

    I attached the public bool to the character instead and then had the game session simply control flicking on or off the bool. Done!
     
    Kurt-Dekker likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    AWESOME! Feels good, doesn't it?

    Here's some more tricks to add to your toolbox:

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494