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

Pistol animation plays even with no ammo

Discussion in 'Scripting' started by doomlazy, Jul 26, 2014.

  1. doomlazy

    doomlazy

    Joined:
    Aug 24, 2013
    Posts:
    44
    Here is my script:

    Code (JavaScript):
    1. var bullets : int;
    2.  
    3. function Start()
    4. {
    5.     bullets = 10;
    6. }
    7.  
    8. function Update ()
    9. {
    10.     if(bullets > 0 && Input.GetMouseButtonDown(0))
    11.     {
    12.         animation.Play("Shoot");
    13.     }
    14.    
    15.     if (Input.GetKeyDown(KeyCode.R))
    16.     {
    17.         ReloadGun();
    18.     }
    19. }
    20.  
    21. function ReloadGun()
    22. {
    23.     yield WaitForSeconds (0.8);
    24.     bullets = 10;
    25. }
    For some reason even when bullets = 0 the animation still plays. any help?
    Thanks for reading!
     
  2. Zackhanzo

    Zackhanzo

    Joined:
    Jun 5, 2014
    Posts:
    65
    In that code, you will always have 10 bullets. You have to take -1 to bullets each time you shoot. (I guess you are putting the whole code due to you have the reload function attached in here too)
     
  3. doomlazy

    doomlazy

    Joined:
    Aug 24, 2013
    Posts:
    44
    I can't believe I forgot to subtract the bullets xD
     
  4. Faestus

    Faestus

    Joined:
    Jul 23, 2014
    Posts:
    68
    Try adding "Debug.Log(bullets)" in the update.
     
  5. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    Code (JavaScript):
    1. function ReloadGun()
    2. {
    3.     yield WaitForSeconds (0.8);
    4.     bullets = 10;
    5.     animation.Stop("Shoot");
    6. }
    Sorry if it's already answered ^^
     
  6. zDemonhunter99

    zDemonhunter99

    Joined:
    Apr 23, 2014
    Posts:
    478
    Uh.... It's already been answered. And that's not the way of doing it....
     
  7. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    Oh yeha , I failed at the yield part ^^