Reputation: 91
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");
}
}
Upvotes: 6
Views: 27089
Reputation: 7346
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() ;
}
Upvotes: 3