Forum rules - please read before posting.

Right clicks in menu being ignored?

I'm trying just to see when a player right clicks over a specific set of menu buttons, and can't seem to get it working. I'm sure there's better ways to script it, but at the moment I just have an actionList that's called on for these buttons which begins with a custom action I made. This checks if the button pressed was a right click or left, pretty straight forward code added to the template:

if (Input.GetMouseButtonDown (0))
            {
                myVar.BooleanValue = false;
            } else {
                myVar.BooleanValue = true;
            }

This seems to work as it should with a left click, but right clicks just don't seem to do anything. I must be missing something obvious here - is there something I'm unaware of with right clicks and menus? Are they blocked somehow? Or is there a better way to be doing this? Sorry for such a dull question, I'm really hopeless at writing my own custom scripting despite how much I read :neutral:

Comments

  • Reading input within an ActionList should generally be avoided, as it requires the Action to be running at the exact time you need to detect the input.

    What's the Menu's Source property set to? The generic way to do this would be to have a regular MonoBehaviour script hook into the OnMouseOverMenu custom event, to check if the Button in question is currently hovered over:

    using UnityEngine;
    using AC;
    
    public class DetectRightClick : MonoBehaviour
    {
    
        private bool isSelected;
    
        private void OnEnable () { EventManager.OnMouseOverMenu += OnMouseOverMenu; }
        private void OnDisable () { EventManager.OnMouseOverMenu -= OnMouseOverMenu; }
    
        void OnMouseOverMenu (Menu _menu, MenuElement _element, int _slot)
        {
            isSelected = _menu.title == "MyMenu" && _element.title == "MyButton";
        }
    
        void Update ()
        {
            if (Input.GetMouseButtonDown (1) && isSelected)
            {
                Debug.Log ("Right click detected.");
            }
        }
    
    }
    

    If you're using Unity UI, though, you can instead just attach a more simple component to the Button itself, which can implement Unity's IPointerClickHandler interface to detect right-clicks directly:

    using UnityEngine;
    using UnityEngine.EventSystems;
    
    public class DetectRightClick : MonoBehaviour, IPointerClickHandler
    {
    
        public void OnPointerClick (PointerEventData eventData)
        {
            if (eventData.button == PointerEventData.InputButton.Right)
            {
                Debug.Log ("Right click detected.");
            }
        }
    
    }
    
  • Thanks so, so much for the reply Chris, I was trying code similar to this yesterday but looking at this I understand where I went wrong.

    I'm having trouble getting past an error now though that occurs as soon as I move the mouse over the menu named in my script. I should clarify, the source of the menu is indeed AC. The error is: NullReferenceException: Object reference not set to an instance of an object. This points to the script line: isSelected = _menu.title == "EviRecordedDialogue" && _element.title == "test1";. I worked out that it seems to be the menu element causing the issue, if I take that out there's no error. I made different elements to test it with, but I can't figure out what the issue with it is.

    Full script used:

    using UnityEngine;
    using AC;
    
    public class EviConvRightClick : MonoBehaviour
    {
    
        private bool isSelected;
        public ActionList actionList;
    
        private void OnEnable () { EventManager.OnMouseOverMenu += OnMouseOverMenu; }
        private void OnDisable () { EventManager.OnMouseOverMenu -= OnMouseOverMenu; }
    
        void OnMouseOverMenu (Menu _menu, MenuElement _element, int _slot)
        {
            isSelected = _menu.title == "EviRecordedDialogue" && _element.title == "test1";
        }
    
        void Update ()
        {
            if (Input.GetMouseButtonDown (1) && isSelected)
            {
                actionList.Interact ();
            }
        }
    
    }
    

    Your help and time is very much appreciated Chris!

  • Place this at the front of the OnMouseOverMenu:

    if (_element == null) return;
    
  • Beautiful, works like a charm!! You're a lifesaver B)

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.