Simple Script To Press A Button?

Im trying to figure out a simple code to make a script automatically press a button (for example R) without the user actually pressing it themselves, something like:

var bullets = 5;

function Reload(){

  if(bullets>1){
//a code to press R without the user actually pressing it...
  }
}

Thanks in advance.

Make a function that is called when the button is pressed, and when bullets > 1.

No but I need a script that presses a button by itself, nothing that is called when that button is pressed. Its hard to explain but I want something like

(Input.GetKeyDown(KeyCode.R))

Without the actual Input from the user.

Yeah, you would do:

if(Input.GetKeyDown(KeyCode.R))
{
myFunction();
}

if(bullets < 1)
{
myFunction();
}
}

function myFunction()
{
bullets = 5;
}

Only way to do it in Unity (that I know of).

Doesnt seem to work, youd have to press R manually either way, not sure how to solve this :confused:

No you wouldn’t.
The function would execute if bullets was less than 1.

Tried it and it didnt work :frowning:

//----------------------------------------------
if(Input.GetKeyDown(KeyCode.R)) 
{ 
myFunction(); 
} 

if(x == 5) 
{ 
myFunction(); 
} 
//----------------------------------------------
   
}
//----------------------------------------------
var x = 5;
function myFunction() 
{ 
x = 5; 
}
//----------------------------------------------

Err… x always = 5 in that script, nothing would change.

Ugh, lets just ignore all those stupid variables, i just put it in for the code to make sense…

function Reload(){
bulletsleft = bulletsleft + bullets;
//a code to press R automatically without the user actually pressing R
}

Basically all i need is the part that has the comment, I dont want any variables to change or anything such as that, just the script to automatically press R.

To do that, you would need to use an external Windows dll.

The only way to do it in Unity is via functions.

EDIT:

Might be what you’re looking for.

EDIT2:
Seems as though that’s windows forms only.

You cannot do what you are asking. You do not need to do what you are asking either, was the point the others are making in this thread.

If you can execute code if the key is down, you can execute that same code under some other conditional.

You can either take the code you want to execute when the user presses R and when you do this reload function into its own function and just call it under both of those conditionals, or just physically copy the code into both of those places (which is fairly inefficient, but it will work.)

If you can’t get this to work, just post the code you have for both the function you are trying to get working and the code you have to handle when the user presses the R key, and it will be easier to work on the problem.

There might also be a problem if the functions are in different classes, but that is not insurmountable.

Well the only reason why im asking for this is because in my game, when my gun runs out of bullets, then I pick up more, the reload sequence runs, but it doesnt actually reload UNTIL you press R, or LMB (i really dont know why) So in this case if it presses R by itself, then I wont have that issue.

I can post up the script if that will help, but its fairly long…

I totally agree with the other posters.

Either we’re not getting what you really want, or you’re not doing what the other posters are recommending.

Here’s the logic (didn’t check the code, just wrote it to demonstrate the logic):

FireScript.JS

private var bullets : int = 20; // bullets ready to fire
private var totalBullets : int = 200; // total bullets the player has
private var magazineSize : int = 50; // how many bullets each magazine has
private var pickupBulletAmount : int = 100; // how many bullets each pickup has

function Update()
{
    // check if the user pressed 'R'
    if (Input.GetKeyDown(KeyCode.R)) {
        // user pressed 'R'
        // run reload code (see function at the end of script)
	    Reload();
	}
	
	// check if the user has ran out of bullets
	if (bullets <= 0) {
		// player has ran out of bullets...
		// check if we have enough bullets... if so,
		// let's reload some more
		// to do that, we run the reload code
		Reload();
	}
}

function OnTriggerEnter(other : Collider)
{
	// check if the other collider is a bullet pickup
	if (other.tag == "Bullets") {
		// the player picked up some more bullets
		// add those bullets to the player's reserve
		totalBullets += pickupBulletAmount;
		// now check if the player has ran our of bullets
		// if he has, force a reload
		if (bullets <= 0) {
			Reload();
		}
	}
}

function Reload()
{
	// do all necessary actions to reload, like
	// play the reloading animation of the gun
	// add bullets to the current magazine
	bullets += magazineSize;
	// reduce the total number of bullets
	totalBullets -= magazineSize;
	// play the reloading sound
	// etc.
}

I am coming accross this post years later. At least now there seems to be a simple one line solution:
Let the button be called myButton. The line that does the trick then is

myButton.onClick.Invoke();