1
\$\begingroup\$

I am new to Unity and C#. I have a script here for a card game where my card is supposed to move up and down when the pointer is hovered over the card. But when my point hovers over each card, they all move up together and not separately. They also move when they touch other UI elements, regardless of what they are. I am doing something wrong but I don't know what.

Here is my code:

//My Codes I needs help on.
   using UnityEngine;
   using System.Collections;
   using UnityEngine.EventSystems;

   public class Mouse_Detector_Temp : MonoBehaviour {
    public bool hovered;
    // Use this for initialization
    void Start ()
    {
        hovered = false;
    }

    // Update is called once per frame
    void Update ()
    {
        //This checks to see if you are hovered over.
        if (EventSystem.current.IsPointerOverGameObject ()) 
        {
            Debug.Log(gameObject);
            if (hovered == false)
            {
                transform.Translate(0,10,0);
                hovered = true;
            }
        }
        else
        {
            if (hovered == true)
            {
                transform.Translate(0,-10,0);
                hovered = false;
            }
        }
    }

}

Additional Information: This code is run UI elements that are part of a Canvas (only way to do it, so that should be obvious).

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

I am not quite sure what this does:

if (EventSystem.current.IsPointerOverGameObject ()) 

But for me it seems as if this method just checks, if the pointer hovers over any gameobject. And as you are checking this, for each card, in each frame, I think it is obvious, why you get that effect. Again: this only holds if the method does what I think.

You can overcome this problem by somehow checking, if the hovered object is also the actual object. And as you already have an event system, it would be nicer to register all the cards to an hover event and just do the moving in the update method.

\$\endgroup\$
3
  • \$\begingroup\$ This is going to sound very noob of me. But I am not sure how to write something that would identify each card. I understand what it is doing. I have no idea how to rewrite this code so it works for a single card. I had though that "current" was what I needed for that but apparently that doesn't work like I thought. And I can't seem to figure out how to use "this" if it even applies. Additional Info: Each card runs this script. \$\endgroup\$ Commented Jul 14, 2015 at 13:45
  • \$\begingroup\$ The "current" seems to just give you the current active EventSystem. I found something that could help you. It seems as if you have to do some raycasting: stackoverflow.com/questions/27888293/… I am sorry, I just had a quick look into Unity a few months ago, so I am not totally sure how you need to do this. I saw that this script is run by each card. That is where the problem also comes from. Each card just checks if ANY card is hovered and then animates itself. \$\endgroup\$ Commented Jul 14, 2015 at 16:52
  • \$\begingroup\$ I did some research, and raycasting doesn't work on UI elements since there is nothing to collide with. I gave it a 2D box collider but the "hitbox" didn't match what I had used (Since the image I had used moved with the canvas, the 2D box collider did not move I assume.) Is there anyway I can use a UI.Graphic.Raycast()? I have been trying to use a Graphic.Raycast but it doesn't seem to be working either. I feel completely helpless on this, because my research on this isn't coming out very fruitful. \$\endgroup\$ Commented Jul 16, 2015 at 10:34
1
\$\begingroup\$

I figured out how to fix this myself. This took me days just to find an answer to this question. I hope other people who find this question can use this answer.

//My Codes I needs help on.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;

public class Mouse_Detector_Temp : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler {
    private bool hovered;

    // Use this for initialization
    void Start ()
    {
        hovered = false;

    }

    // Update is called once per frame
    void Update ()
    {

    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log(gameObject);
        if (hovered == false)
        {
            transform.Translate(0,10,0);
            hovered = true;
        }
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        transform.Translate(0,-10,0);
        hovered = false;
    }
}

First I found out. For graphics, you need to use OnPointerEnter and OnPointerExit instead of EventSystem.current.IsPointerOverGameObject ()

And IT WASN'T WORKING, because... I was not aware I needed to add this:

public class Mouse_Detector_Temp : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler

To use OnPointerEnter and OnPointerExit.

I figured it out.

\$\endgroup\$
1
  • \$\begingroup\$ What this code does: Is it moves my card up when it is hovered over, and it moves it back down when it is not. \$\endgroup\$ Commented Jul 19, 2015 at 2:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .