Search Unity

Why is this function call not working ?

Discussion in 'Getting Started' started by CyberInteractiveLLC, Feb 16, 2018.

  1. CyberInteractiveLLC

    CyberInteractiveLLC

    Joined:
    May 23, 2017
    Posts:
    307
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         Shooting (shoot1Input, PickupsManager.m_rifle.infiniteAmmo, PickupsManager.m_rifle.currentAmmo, PickupsManager.m_rifle.ammoText);
    4.  
    5.  
    6.     }
    7.  
    8.     public void Shooting(bool Key, bool infiniteAmmo, int currentAmmo, Text txt)
    9.     {
    10.         if (Key) {
    11.             if (currentAmmo > 0 && !infiniteAmmo) {
    12.                 currentAmmo = currentAmmo - 1;
    13.  
    14.                 StartCoroutine (PickupsManager.TextFlash (PickupsManager.textFlashSpeedShooting, PickupsManager.textFlashCountShooting, txt));
    15.  
    16.             } else if (infiniteAmmo) {
    17.                 currentAmmo = 999;
    18.  
    19.                 StartCoroutine (PickupsManager.TextFlash (PickupsManager.textFlashSpeedShooting, PickupsManager.textFlashCountShooting, txt));
    20.             } else {
    21.                 Debug.Log ("No Ammo");
    22.             }
    23.         }
    24.     }
    I don't really get it, i've stared at this code for a hour now.. and i'm not really sure, I have this code that does the exact same thing and it works fine (below)

    Code (CSharp):
    1.     void Update()
    2.     {
    3.  
    4.         if (Input.GetKeyDown (KeyCode.Space)) {
    5.          
    6.             if (PickupsManager.m_rifle.currentAmmo > 0 && !PickupsManager.m_rifle.infiniteAmmo) {
    7.                 PickupsManager.m_rifle.currentAmmo = PickupsManager.m_rifle.currentAmmo - 1;
    8.  
    9.                 StartCoroutine (PickupsManager.TextFlash (PickupsManager.textFlashSpeedShooting, PickupsManager.textFlashCountShooting, PickupsManager.m_rifle.ammoText));
    10.             } else if (PickupsManager.m_rifle.infiniteAmmo) {
    11.                 PickupsManager.m_rifle.currentAmmo = 999;
    12.                 StartCoroutine (PickupsManager.TextFlash (PickupsManager.textFlashSpeedShooting, PickupsManager.textFlashCountShooting, PickupsManager.m_rifle.ammoText));
    13.             } else {
    14.  
    15.                 Debug.Log ("Ammo at 0");
    16.             }
    17.         }
    but when i add values to the function to make my life easier, it's not calling it? not even the debug.log is being called.. so i'm not sure what is wrong
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hm. Is 'key' false?
     
  3. CyberInteractiveLLC

    CyberInteractiveLLC

    Joined:
    May 23, 2017
    Posts:
    307
    Code (CSharp):
    1.     void Update()
    2.     {
    3. shoot1Input = Input.GetKeyDown (shootKey);
    4.         Shooting (shoot1Input, PickupsManager.m_rifle.infiniteAmmo, PickupsManager.m_rifle.currentAmmo, PickupsManager.m_rifle.ammoText);
    5.     }
    6.     public void Shooting(bool Key, bool infiniteAmmo, int currentAmmo, Text txt)
    7.     {
    8.         if (Key) {
    9.             if (currentAmmo > 0 && !infiniteAmmo) {
    10.                 currentAmmo = currentAmmo - 1;
    11.                 StartCoroutine (PickupsManager.TextFlash (PickupsManager.textFlashSpeedShooting, PickupsManager.textFlashCountShooting, txt));
    12.             } else if (infiniteAmmo) {
    13.                 currentAmmo = 999;
    14.                 StartCoroutine (PickupsManager.TextFlash (PickupsManager.textFlashSpeedShooting, PickupsManager.textFlashCountShooting, txt));
    15.             } else {
    16.                 Debug.Log ("No Ammo");
    17.             }
    18.         }
    19.     }
    Ok so i needed to move this shoot1Input = Input.GetKeyDown (shootKey); from Start function to Update function

    now Coroutines are working but "current = current - 1" are not. infinite ammo is false
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, all I can say is that you should Debug.Log some values to verify the values are correct.

    A small observation is that you could modify your code a bit, so it's like this:
    Code (csharp):
    1. void Update()
    2.     {
    3.         if(Input.GetKeyDown (shootKey))
    4.            Shooting (PickupsManager.m_rifle.infiniteAmmo, PickupsManager.m_rifle.currentAmmo, PickupsManager.m_rifle.ammoText);
    5.     }
    6.     public void Shooting(bool Key, bool infiniteAmmo, int currentAmmo, Text txt)
    7.     {
    8.   /* Add this, maybe? */
    9.         Debug.Log("Current ammo: " + currentAmmo.ToString());
    10.           if (currentAmmo > 0 && !infiniteAmmo)
    11.           {
    12.                 currentAmmo = currentAmmo - 1;
    13.                 StartCoroutine (PickupsManager.TextFlash (PickupsManager.textFlashSpeedShooting, PickupsManager.textFlashCountShooting, txt));
    14.          }
    15.          else if (infiniteAmmo)
    16.          {
    17.              currentAmmo = 999;
    18.              StartCoroutine (PickupsManager.TextFlash (PickupsManager.textFlashSpeedShooting, PickupsManager.textFlashCountShooting, txt));
    19.           }
    20.           else {
    21.                 Debug.Log ("No Ammo");
    22.             }
    23.     }
    I just thought that might make a little more sense, seeing as all you used that first parameter for was to check if it was true. :)

    Pardon the horrible indenting amounts, it's a bit tedious fixing them every time.
     
  5. CyberInteractiveLLC

    CyberInteractiveLLC

    Joined:
    May 23, 2017
    Posts:
    307
    Yea the log shows correct values, i just cannot change them immediately, so i updated it and wrote this


    Code (CSharp):
    1.         if (shoot1Input) {
    2.             Shoot2 (PickupsManager.m_rifle.infiniteAmmo, PickupsManager.m_rifle.currentAmmo, "Rifle", PickupsManager.m_rifle.ammoText);
    3.         }
    4.  
    5.     public void Shoot2(bool infiniteAmmo, int currentAmmo, string weapoName, Text txt)
    6.     {
    7.        
    8.  
    9.         if (currentAmmo > 0 && !infiniteAmmo) {
    10.  
    11.             currentAmmo = currentAmmo - 1;
    12.  
    13.  
    14.         } else if (infiniteAmmo) {
    15.  
    16.             currentAmmo = 999;
    17.         } else {
    18.             Debug.Log ("No Ammo");
    19.         }
    20.  
    21.         if (weapoName == "Rifle") {
    22.             PickupsManager.m_rifle.currentAmmo = currentAmmo;
    23.         }
    24.     }
    I don't like this code because i'll have to keep checking weaponName in the Shoot function but atleast it works lol
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ya, I can imagine.

    One way to handle it could be to use a 'Shoot' method on the gun itself that handles it's own ammo.
     
  7. CyberInteractiveLLC

    CyberInteractiveLLC

    Joined:
    May 23, 2017
    Posts:
    307
    I will probably do that, I usually prefer to have 1 manager script for everything but i guess it's better put this script on all weapon prefabs
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Maybe I don't know enough about your game. If Ammo can be shared equally by any weapon/rifle, then maybe it matters less. If the ammo is unique to an item, though, then it makes sense that you want a place to track it.
    I suppose you could pass the weapon/rifle to the method, as well. Your code was passing variable of the rifle to the method, but perhaps just the rifle could then read & write to it.

    In any event, I didn't mean to break out into a number of ideas, just trying to agree/assist on not wanting to check strings like "rifle" in your post :)