Search Unity

Why is my code returning a false amount?

Discussion in 'Scripting' started by Pappi11, Nov 5, 2017.

  1. Pappi11

    Pappi11

    Joined:
    Mar 27, 2014
    Posts:
    9
    I cant get my head worked around this.. This is working fine on every other weapon i have but not with this one.. here is my code


    Code (CSharp):
    1.  if (Input.GetButtonDown("Reload"))
    2.         {
    3.        
    4.                 int asd;
    5.                 asd = ClipSize - AmmoManager.M4Clip;
    6.  
    7.                 if (asd <= AmmoManager.M4Ammo)
    8.                 {
    9.                     AmmoManager.M4Clip = 30;
    10.                     AmmoManager.M4Ammo = AmmoManager.M4Ammo - asd;
    11.  
    12.  
    13.  
    14.                 }
    15.                 else if (AmmoManager.M4Ammo > 0)
    16.                 {
    17.                 Debug.Log(AmmoManager.M4Clip + AmmoManager.M4Ammo);
    18.  
    19.                 AmmoManager.M4Clip = AmmoManager.M4Ammo + AmmoManager.M4Clip;
    20.  
    21.                     Debug.Log(AmmoManager.M4Clip + AmmoManager.M4Ammo);
    22.  
    23.                     AmmoManager.M4Ammo = 0;
    24.  
    25.  
    26.                 }
    27.         }
    When i have in example 20 bullets left in my clip and 9 bullets left on my AmmoManager.M4Ammo it returns a value of 9 instead of 29... Im so confused..
     
    Last edited: Nov 5, 2017
  2. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    gjf likes this.
  3. Pappi11

    Pappi11

    Joined:
    Mar 27, 2014
    Posts:
    9
  4. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Code (CSharp):
    1. //true test
    2.  
    3. int asd;
    4. asd = ClipSize - AmmoManager.M4Clip; //clipSize = 30 - current M4clip(20); asd and should = 10
    5.  
    6. if (asd <= AmmoManager.M4Ammo) 10 <= m4Ammo,  hmmmm let say m4Ammo = 50
    7. {//this is true
    8.         AmmoManager.M4Clip = 30; //set m4clip to 30

    9.         AmmoManager.M4Ammo = AmmoManager.M4Ammo - asd; //seto m4Ammo to be 50 - 10, so M4ammo = 40

    10. }else if (AmmoManager.M4Ammo > 0)
    11. {//doesn’t do this
    12.     Debug.Log(AmmoManager.M4Clip + AmmoManager.M4Ammo);
    13.     AmmoManager.M4Clip = AmmoManager.M4Ammo + AmmoManager.M4Clip;
    14.     Debug.Log(AmmoManager.M4Clip + AmmoManager.M4Ammo);
    15.     AmmoManager.M4Ammo = 0;
    16. }
    17.  
    18. //false test
    19.  
    20. int asd;
    21. asd = ClipSize - AmmoManager.M4Clip; //clipSize = 30 - current M4clip(20); asd and should = 10
    22.  
    23. if (asd <= AmmoManager.M4Ammo) 10 <= m4Ammo,  hmmmm let say m4Ammo = 5
    24. {
    25. //this is false, does not do this
    26. AmmoManager.M4Clip = 30;
    27. AmmoManager.M4Ammo = AmmoManager.M4Ammo - asd;
    28. }else if (AmmoManager.M4Ammo > 0) m4ammo > 0,  hmmmm let say m4Ammo = 5
    29. {//this is true
    30.     Debug.Log(AmmoManager.M4Clip + AmmoManager.M4Ammo); // 20 + 5, should print 25

    31.     AmmoManager.M4Clip = AmmoManager.M4Ammo + AmmoManager.M4Clip; //set m4clip to 25

    32.     Debug.Log(AmmoManager.M4Clip + AmmoManager.M4Ammo);  // 25 + 5, should print 30

    33.     AmmoManager.M4Ammo = 0;  //set m4ammo to 0

    34. }
     
  5. Pappi11

    Pappi11

    Joined:
    Mar 27, 2014
    Posts:
    9
    Ok ill try something like this, thanks.
     
  6. Pappi11

    Pappi11

    Joined:
    Mar 27, 2014
    Posts:
    9
    I dont get this, still every time i run this code

    Code (CSharp):
    1. Debug.Log(AmmoManager.M4Clip + AmmoManager.M4Ammo);
    2.  
    3.                 AmmoManager.M4Clip = AmmoManager.M4Ammo + AmmoManager.M4Clip;
    4.                 Debug.Log(AmmoManager.M4Clip + AmmoManager.M4Ammo);
    5.  
    6.                 AmmoManager.M4Ammo = 0;  
    I only get the value of M4Ammo or M4Ammo+M4Ammo.. Im confused.. xd
     
  7. Pappi11

    Pappi11

    Joined:
    Mar 27, 2014
    Posts:
    9
    Even if i store the current m4Clip in to a different function ( in ex "int ClipBeforeReload) and then run the code like this
    Code (CSharp):
    1. Debug.Log(AmmoManager.M4Clip + AmmoManager.M4Ammo);
    2.  
    3.                 AmmoManager.M4Clip = AmmoManager.M4Ammo + ClipBeforeReload;
    4.  
    5.                 Debug.Log(AmmoManager.M4Clip + AmmoManager.M4Ammo);
    6.  
    7.                 AmmoManager.M4Ammo = 0;  
    I still get a result with only The m4Ammo or M4Ammo x2..
     
  8. Pappi11

    Pappi11

    Joined:
    Mar 27, 2014
    Posts:
    9
    Ok i dont know what was causing the issue but i just changed all of my m4 function names to m4ammo1 m4clip1 etc and it works fine now... Well thanks for the help anyway.