0

I am writing a game that includes the combo system (it's a script). I currently have it so that the inputs are keys, but I want to change it so it takes button pressing input instead.

Here is the input code:

void Update() {
         if (Input.GetKeyDown(KeyCode.E))
            input = new ComboInput(AttackType.heavy);
         if (Input.GetKeyDown(KeyCode.R))
            input = new ComboInput(AttackType.light);
}

The thing about UI buttons is that they take an onPressed() command, but I cant make a function to give this input because its already in an update function. Any help would be appreciated.

1 Answer 1

1

You can add a delegate for the button to call it when it has clicked.

Example:

private void Awake()
{
    Button buttonComponent = GetComponent<Button>();
    buttonComponent.onClick.AddListener(OnButtonClicked)
}

private void OnButtonClicked()
{
    Debug.Log("The button has clicked!");
}

You have to attach this script to your Button gameobject.

Also you can add the event function directly to your button directly via inspector, but the function should be public in order to be accessed by inspector. If you don't want your function to be seen by other classes, then the above would be your best choice.


In your case, Here's my suggestion:

[SerializeField]
private Button buttonE, buttonR;

private ComboInput input;

private void Awake()
{
    buttonE.onClick.AddListener(OnButtonClicked_E);
    buttonR.onClick.AddListener(OnButtonClicked_R);
}

private void OnButtonClicked_E()
{
    input = new ComboInput(AttackType.heavy);
}

private void OnButtonClicked_R()
{
    input = new ComboInput(AttackType.light);
}

EDIT: If what you want is editing your existing combat script (not creating a new individual component), then you have to give your buttons to your combat script. That is buttonE, buttonR, which you have to give a reference via unity inspector. If it has done, then you're free to remove your Update() function, if there are no other things related to it.

Sign up to request clarification or add additional context in comments.

3 Comments

alright, makes sense. But how would I implement this in a combat script?
oh, you seemed to have misunderstood me. I currently have it so you have to click keys E and R to do the input, but I want to get rid of that and make it so I can click the buttons instead. I'll go edit the question.
yeah a combat script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.