I'm very new to scripting in Unity, I'm trying to create a button, and once clicked it needs to simulate the 'F' Key being pressed (To pick up an item)
Here is the current code I have, I've looked all over unity forums before writing this but couldn't find anything that worked.
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
public class button : MonoBehaviour {
public void ButtonToClick(int clickToButton)
{
SendKeys.Send("F");
}
}
You can simulate a button click by sending a NavigationSubmitEvent with your button as the target. Code (CSharp): using (var e = new NavigationSubmitEvent() { target = WButton } )
A button should only react once it is clicked, which is why it has the onclick . Unity is already internally checking if a button is clicked by using its EventSystem and a series of GraphicalRaycasts .
I believe simulating the key press is not the right way to do it.
Instead, you should call the PickUp
function when the button is clicked the same way Pickup
is called when the F
key is pressed.
// Drag & Drop the object holding the script to the `OnClick` listener of your button
// Then, simply select the `Pickup` function
public void Pickup()
{
// code ....
}
private void Update()
{
if( Input.GetKeyDown( KeyCode.F ) )
Pickup() ;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With