Disable item after 10 shots.

Hello how can I disable an object after you press 10 times the left-muose? This is the script that I’m using, but does not work very well.

My script.

#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);
     
    
 }

You need to become more comfortable with your use of if statements.

As it stands, here’s what’s happening:

You click, your ammo is set to 10. But that’s all your if statement is doing. Then, every frame, you subtract 1 from your current ammo (so, either 10 becomes 9 or 0 becomes -1), then you set it straight to 0.

You’re already setting your ammo count to 10 in the editor. When you click/fire, reduce your current count by 1 (i.e. “countAmmo -= 1” or “countAmmo = (countAmmo - 1)” as you already did), then check whether you have any ammo left.

...

if(Input.GetButtonDown("Fire1"))
{
     if(countAmmo > 0)
     {
          // Instantiate a projectile or whatever the means is to fire.
          countAmmo -= 1;
     }
     else // if(countAmmo <= 0) -- Commented out because it's unnecessary, but there to show intent
     {
          // You have no ammo!
     }
}

...

The important part is in here, now u must adapt it

if(Input.GetButtonDown(“Fire1”)){//checks for input

  countAmmo--;//count ammo - 1
  if(countAmmo <= 0){//if cound ammo is less or iqual to 0
     myObject.SetActive(false);//turn off object
  }

ps: You should pratice a little more your programing skills