Right Click problem

Hi there,

This is my first post here so I dont know if this is the correct area or the correct way to right my question. I am trying to create a talent tree… Actually I have it ready and what I want is when the user hit the right click to decrease the talents that he spent in one talent point

(For example if he has 1 talent point that spent 5 point when he hit the right click to decrease the talent point and add it in the total_skill_points)

This is the part of my code that increase or decrease the talents

public void OnClick (int value){

        if ( skills[value].SkillPointsAdded < skills[value].SkillPointReq && skillPoints > 0 ){
            skills[value].SkillPointsAdded ++ ;
            skillPoints --;
            TalentAdd(value);
            Total ++;
            Debug.Log("Your total points are " + Total);
        }
    }

    public void OnRightClick (int value){
        if (skills[value].SkillPointsAdded > 0){

            this.skills[value].SkillPointsAdded--;
            Debug.Log ( this.skills[value].SkillName + "" +this.skills[value].SkillPointsAdded);
            this.skillPoints ++;
            this.Total -- ;

        }
    }

The problem is that when I add the script on multiple UI buttons and right click somewhere everything is decreased. I need only to decrease the one that I am clicking…

Also I use a custom RightEventHandler so I am copying that cs also here :

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using System.Collections;


[ExecuteInEditMode]
[AddComponentMenu("Event/RightButtonEvent")]
public class RightButtonEvent : MonoBehaviour , IPointerEnterHandler, IPointerExitHandler{

    [System.Serializable] public class RightButton: UnityEvent{}
    public RightButton onRightDown;
    public RightButton onRightUp;
    private bool isOver = false;
    // Use this for initialization
    void Start () {
     
    }
 
    // Update is called once per frame
    void Update () {
        if(Input.GetMouseButtonDown (1)){
            onRightDown.Invoke();
        }
        if (Input.GetMouseButton(1)){
            onRightUp.Invoke ();
        }
    }

    public void OnPointerEnter(PointerEventData eventData){
        isOver = true;
    }

    public void OnPointerExit(PointerEventData eventData){
        isOver = false;
    }
}

Thank you in advance.

First, make sure you read this sticky post Using code tags properly - Unity Engine - Unity Discussions

Your problem seems to be that you’re not checking if the mouse is over the element, only if the right button is clicked. You probably want to do something like

if(Input.GetMouseButtonDown (1) && isOver)

You could also probably just use IPointerClickHandler, it should be simpler.

2 Likes

Thank you very very mutch!!! This thing solved my problem and now only the one i am pointing decrease its value… But can you explain me why???

He just did explain why. You were only checking “If the right mouse button is clicked, decrease”, which isn’t specific to a certain button/skill.

1 Like

Kill your custom right click handler. It’s a horrible idea. The built in click handler sends pointer event data that can be used to detriment if it is a right or left click.

I think @Kiwasi is referring to these

http://docs.unity3d.com/ScriptReference/EventSystems.IPointerClickHandler.OnPointerClick.html
http://docs.unity3d.com/ScriptReference/EventSystems.PointerEventData-button.html

1 Like