- Home /
 
Increasing A Value Keeps Doubling?!
Hello,
I have a script that detects if my raycast is clicked on a certain object, and when it is clicked, it increases a value by += 1. The problem is, is that my value is increase by 2, not 1?!
Here is my script:
 if(hit.collider.name == "DropUnlock(Clone)" || hit.collider.name == "DropUnlock"){
    print("Drop Unlock");
     if(Input.GetButtonUp("Fire1")){
         Destroy(hit.collider.gameObject);    
         Values.unlocks = Values.unlocks + 1;
     }
 }
 
               Lets say my unlock is currently at 4. When the user clicks on the DropUnlock object, the unlock value changes from 4 to 6, not 5.
Why is this happening?
Thanks
Because you're running it twice, I would guess. By the way, you can do x += 1 ins$$anonymous$$d of x = x + 1, or in the case of x += 1, you can do x++.
How am I running it twice? This is inside my Update Function
I don't know, I don't have enough info. :) $$anonymous$$aybe you have the script attached twice.
Ah yeah, silly me. I did have the same script twice within my scene. Thanks
Answer by robert_mathew · Feb 26, 2012 at 06:04 AM
 var p_hit_break : int;
 function Start ()
    {
   p_hit_break =1; 
    }
  if(hit.collider.name == "DropUnlock(Clone)" || hit.collider.name == "DropUnlock"){
  print("Drop Unlock");
   if(Input.GetButtonUp("Fire1") && p_hit_break ==1 ){
       p_hit_break =2;
       Destroy(hit.collider.gameObject); 
       Values.unlocks = Values.unlocks + 1;
        }
           }
 
               by this method is that my value is increase by 1 as soon as it enter the loop it will become false when ever you want again this condition to work make p_hit_break ==1 so that the condition become true.
Your answer