Search code examples
unity-game-engineunityscript

Disable item after 10 shots


How can I disable an object after you press 10 times the left mouse button? This is the script that I'm using, but does not work very well.

#pragma strict

var myTrigger : GameObject;
var myObject : GameObject;
var countAmmo : int = 10 ;

function Start()
{
}

function Update()
{
  if(Input.GetButtonDown("Fire1"))

     countAmmo = 10;

        //myObject.SetActive(true);

        countAmmo = (countAmmo -1);

         countAmmo = 0; 

        myObject.SetActive(false);
}

Solution

  • follow proper variable scopes.. try this:

        var countAmmo : int = 10 ;
    
        function Update()
         {
    
          if(Input.GetButtonDown("Fire1"))  
    
             countAmmo = countAmmo -1;        
             if(countAmmo == 0){         
                myObject.SetActive(false);    
             }else{
                myObject.SetActive(true);
             } 
         }
    }
    

    I believe you're trying to reduce the count by one in each button press, until the count is zero . and if no more ammo(count = 0), you need to disable the fire button, right?