Put a Function in void Update problem

I wanted to ask something i think is really simple but i just dont get it.

I want to put my public void OnPointerClick(PointerEventData eventData) in void Update but i dont know hot to because it got something in the brackets.

If somebody could help me i would be really thankfull :wink:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine.UIElements;

public class DifferentButtonClick : MonoBehaviour, IPointerClickHandler
{
    public GameObject panel;
    private void Start()
    {
        panel.SetActive(false);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape) && panel == true || Input.GetKeyDown(KeyCode.I) && panel == true)
        {
            panel.SetActive(false);
        }
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        ////Mouse Middle Click
        if (eventData.button == PointerEventData.InputButton.Middle)
            Debug.Log("Middle Click");
        //Mouse Right Click
        else if (eventData.button == PointerEventData.InputButton.Right)
        {
            panel.SetActive(true);
        }
    }

    public void RightClick(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Right && panel == true)
        {
            panel.SetActive(false);
        }
    }
}

1 Answer

1

You can’t! You also shouldn’t.

Update is for things that happens every frame, regardless of external input. OnPointerClick is something that happens on a specific frame, when the object has been clicked.

So there’s no reason for OnPointerClick to be inside of Update! There’s also technical reasons why it can’t (you can nest methods, and give them the same names, but that’s not going to be a valid method for IPointerClickHandler), but the important thing is that it’s not a thing you’d want to do.

If you want data from OnPointerClick to be available inside of Update (like which mouse button was used last), you can store that information in a field on your class (ie. private int lastButtonClicked;), and use that inside of Update. If that’s relevant! I don’t quite understand what you’re trying to do.

For future reference - it’s a lot easier to help if you explain what you’re trying to achieve. You’re asking the programming equivalent of “How can I attach two planks together using a hot air baloon”, and while I can tell you that that’s not really possible, or even sensible, I can’t point you towards what you should be looking towards instead, since I have no idea why you want the planks to fit together in the first place.

Oh okay thank for the answer and the thig what i want to do is: I got a Inventory and if you right click on an Item it opens a action menu but i want that if you press on another Item right click that the previous action menu closes and the new one pops up, but i have no idea how to do this and wantet to try something.

–

@GerGames you can make another post to ask about how to do you menu / popup system

–