I need help for this script !!

Hi guys. I did this script but the variable “ammo” don’t change and it is ever = 30. (sorry for bad english xD) I don’t get errors in the console…

var ammo : int = 30;
function Update ()
{
        DecreseAmmo();	
	print(ammo);
}

function DecreseAmmo()
{
       if(Input.GetKeyDown(0))
	{
		for (var k = 30; k == 0; k--)
		{
			ammo -= 1;
		}
		
		yield WaitForSeconds(0.125);
	}
}

You need to get a true state for your for-loop, k is not 0 so the loop wont start.

try this:

for(var k = 0; k < 30; k++)
{
   ammo--;
}

also change your seconds to 0.125f

edit now it should work, mistyped a value

Actually Sly, that for-loop won’t start either

for(var k = 0; k < 30; k++)
{
    ammo--;
}

Or in his case:

for (var k = 30; k > 0; k--)
{
    ammo -= 1;
}

Sorry but, it don’t work…

Have you tried moving the input into Update?

function Update ()
{
    if(Input.GetKeyDown(0))
    {
        DecreseAmmo();  
    }
    print(ammo);
}

function DecreseAmmo()
{
        for (var k = 30; k == 0; k--)
        {
            ammo -= 1;
        }
        yield WaitForSeconds(0.125);
    }
}

did you want it to decrease ammo by 1 every 0.125f secs?

that’d be something more like:

<PSEUDO CODE!!>

while(ammo > 0  Input.GetKeyDown(0))
{
 ammo--;
yield waitforsecond(0.125f);
}

doing a for loop and then the yield statement means it’ll decrease ammo to 0 in the for loop, then wait 0.125 secs, then do the whole for loop again

edit: are we talking flamethrower? :smile:

Left righty : yes i want it to decrease ammo by 1 every 0.125f secs (1 every bullet in fire rate 8 every 1 sec)
Yes I tried to put in the update but it don’ t work D: ! WAIT ! But the KeyDown is the left click of the mouse??

Dunno if the mousebutton appears within the keydown list…

you could try these:

Input.GetMouseButtonDown(0) (left mouse button = 0, right 1 and middle 2)
or Input.GetMouseButton(0) just try which one fits better.

another way would be using the game settings input over the menu and choose “Fire” this should be the mousebutton event

ah yeah, what Sly said… I just copy/pasted chunks from the example code above as an example of the kind of logic loop needed… I should probably keep “real code” looking snippets out of pseudo code in future :slight_smile:

This is the final script (but it don’ t work :frowning: ) I try to create a script for the Reload with this Unity Asset Store - The Best Assets for Game Making
var ammo : int = 30;
function Update () {

print(ammo);

if(Input.GetMouseButton(0))

{
DecreseAmmo();
}
function DecreseAmmo()
{

for (var k = 30; k == 0; k–)

{

ammo–;

}

yield WaitForSeconds(0.125f);
}

also tryed this?

for (var k = 30; k > 0; k–)

it works but instead of subtracting the variable ammo 1 it subtracts 60/80 at a time. I don’t know. I do not know. But maybe there is some problem in the other scripts in this package that I downloaded from the assets store.

well your script says when u click the mousebutton it will decrease 30 ammo (due to the for loop) and then wait .125 seconds and does the same… is this what u wanted it to do?

var ammo : int = 30;

function Update ()

{

        DecreseAmmo();  

    print(ammo);

}

 

function DecreseAmmo()

{

       if(Input.GetMouseButtonDown(0))

    {
    	 

        for (var k = 30; k > 0; k--)

        {

            ammo -= 1;

        }

        

        yield WaitForSeconds(0.125);

    }

}

well ammo -= 1; is the same as ammo–;

you could use if(ammo > 0) instead of the for-loop and then remove 1 ammo… I guess you want to remove only 1 ammo per mouseclick or?

It more or less, to be removed at each click of the mouse but if I hold down 1 shot me leverage them in a row.

Then you need to change some stuff within your script.

something like that

var ammo : int = 30;
var waitTime : bool = false;
     
function Update () 
{
   DecreseAmmo();  
   print(ammo); 
}
     
function DecreseAmmo() 
{
   if(Input.GetMouseButtonDown(0))
   {
	// do we have to wait before next ammo reduction?
	if(!waitTime  ammo > 0)
	{     
	   ammo -= 1;
	}
	// wait for x seconds and then activate our function again so we only remove 1 ammo every 0.125 seconds
	yield WaitForSeconds(0.125f);
	waitTime = true;
   }
}

It might be that the waitforseconds thing wont work, so you have to use Time.time instead to compare the seconds u need to wait before the next ammo will be reduced

… that’ll only work once… waitTime is never set to false once it’s been set to true…

It works but it reduces only 1 ammo and then does not reduce more. Maybe when I reduced 1 ammo waitTime must return the variable to false.
This is a big problem…

Sorry my mistake, typed this a bit fast and beside doing something different, this should work now:

    var ammo : int = 30;
    var waitTime : bool = false;
         
    function Update ()
    {
       DecreseAmmo();  
       print(ammo);
    }
         
    function DecreseAmmo()
    {
       if(Input.GetMouseButtonDown(0))
       {
        // do we have to wait before next ammo reduction?
        if(!waitTime  ammo > 0)
        {    
           ammo -= 1;
           waitTime = true;
        }
        // wait for x seconds and then activate our function again so we only remove 1 ammo every 0.125 seconds
        yield WaitForSeconds(0.125f);
        waitTime = false;
       }
    }