Search Unity

Reset float not working

Discussion in 'Scripting' started by serbusfish, May 27, 2017.

  1. serbusfish

    serbusfish

    Joined:
    Dec 27, 2016
    Posts:
    247
    I'm trying to set up a charge shot for a weapon in my game, the player holds down the fire button and releases once the charge has built. However once the charge has built and the shot fired the float I have set up to count to 100 wont reset back to 0. This is my script:

    Code (csharp):
    1.  
    2. void Update ()
    3.  
    4. {
    5.         if (Input.GetButtonDown("Fire5"))
    6.         {
    7.             charging++;
    8.         }
    9.        
    10.         if (Input.GetButtonUp("Fire5"))
    11.  
    12.         {
    13.             if ((Time.time > nextFire) && charging >= 100)
    14.             {
    15.                 nextFire = Time.time + fireRate;
    16.                 Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
    17.                 audio.PlayOneShot(impact, Volume);
    18.                 charging = 0;
    19.              
    20.             }
    21.             else
    22.             {
    23.                 charging = 0;
    24.                 return;
    25.             }
    26.         }
    27.  
    28. }
    29.  
    I thought putting 'charging = 0' would reset the counter but it doesn't. The counter resets as long as the button is released before it reaches 100.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, it looks right to me, so I'm not sure what's up with that :)
     
  3. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    The only thing that I see odd with that script is the return; in the bottom of statement. Not sure why you have that there when it's in Update , and also the very end of your code (nothing will get executed after that in Update...), and it's not returning a value. So unless it's doing something fancy I am unfamiliar with, I'd remove it. Couldn't see it breaking anything.. it shouldn't... But I guess you never know. I assume that's like saying return null or void though so probably fine, just never seen someone do that in a void function or trigger.

    Are you setting this variable somewhere else too, perhaps?
     
    Last edited: May 28, 2017
  4. serbusfish

    serbusfish

    Joined:
    Dec 27, 2016
    Posts:
    247
    I have accidentally found the problem and fixed it. It was related to the audio file funnily enough. Because I had forgot to put 'audio = GetComponent<AudioSource>();' in the Start() this was stopping the float reset from working. Weird, but it works so i'm happy. Also I have removed Return because as you pointed out it was pointless, thanks :)
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ya, if it threw an exception, it would "jump?" out of that area of code's execution ; therefore, not making it far enough :)
    Glad you got it fixed.