# (How exactly to use EventSystem?) Assigning string Name to UI Button in Unity without CrossPlatformInput?

I'm trying to make a new game for Android. There is a Jump button i've added as UI button , this works fine as I have a method for Jump() which I can just add to the OnClick() method in the Button's Inspector tab.

However, for my 'Pick Up' button I don't really want a method for pick up. I already have my item pickup handling code inside a OnTriggerEnter() method and what I was hoping to do was have an additional if condition into my code like:

if (Input.GetButtonDown("Pickup")){ //then it ok to pickup! }


This is how i did it in my other projects. But I had been importing the CrossPlatformInput namespace and then just copied and adapted the buttons from the Jump button that came as standard with that.

So now, I can't see how to name my button as 'Pickup'. I tried looking at the code from CrossPlatformInput, it seems it takes the name you provide it, and passes it to VirtualInput (also in the CrossPlatformInput namespace), I couldnt see a copy of this class anywhere, it think its closed source maybe :/

Of course, I could probably manually make a method to add to the OnClick() which just makes a bool true only when it's being clicked. But I feel like there must be a way to just add this UI button as 'Pickup' to the Input system (like you do with "Horizontal", "Vertical" or "Jump" for example).

Again, I tried to read the docs, and also lots of forums and sites like this. I found that the EventSystem will (or should) have the part I need, but i am now stuck.

I tried this:

EventSystem.current.currentSelectedGameObject.name == ??????????


I read that will return the name of the most recent button pressed, but how would that method know that my new Button is called "Pickup", I dont see how I added it to that yet?

Another reason why i'd like to do it this way is for Cross Platform compatibility. If I make it this way, I already now that there is a certain button on the keyboard mapped in Project>Input to the same name (ie. "Pickup" ; Left-Ctrl). And it seems cleaner to use the existing way of doing this if possible, im sure its just that I dont know how yet.

Most options ive seen online involve the OnClick inspector function, or CrossPlatFormInput imported package.

I hope this makes sense to someone, and that there is a way to do it. Thanks for any help as usual very much appreciated.

Well, it's not really an answer, but I have managed to workaround it by hacking together my own button handling code. It doesn't involve the CrossPlatformInput at all, and it sadly doesn't include any reference to the Unity Project Settings > Input like I was hoping. But it does work for the time being.

If anyone can explain a better way I should of done this, then I'd love to see it:

public class ButtonManager : MonoBehaviour {
public static ButtonManager instance;
public bool isPickUpPressed = false;
private float framesForClick = 3, clickCounter = 0;

private void Awake()
{
instance = this;
}
void Start () { }

void LateUpdate () {
// clears the button pressed bools:
if (isPickUpPressed == true)
{
clickCounter++;
if (clickCounter >= framesForClick)
{
isPickUpPressed = false;
clickCounter = 0;
}
}
}

public void ClickButtonPickUp()
{
isPickUpPressed = true;
}
}


For me this just feels over the top, and wrong.

I can't really say why, I just feel like I should of been able to assign the button with the 'name' of "Pickup" inside the Unity Settings. Instead I just call this ClickButtonPickUp method from my button's OnClick() inspector window.

Setting it to off (or false) involves me making two variables to count the number of frames since the button was pressed and making it back to off, so that it can be clicked again a few frames later. It just seems wrong but its the only way I could personally think of how to do it.

• If this worked for you, don't forget to mark it as the Accepted answer. :) – DMGregory Aug 4 '18 at 18:07