i made a switch script and attached it to a button so that i can switch player just by clicking the button, everything works perfectly but clicking it, triggers the getMouseButtonDown(0) that makes my player jump. how can i make sure that doesn't happen?
Asked
Active
Viewed 845 times
-1
-
i tried using EventSystem.current.IsPointerOverGameObject() but my background is UI too so it doesnt work – Leonardo Moreo Oct 17 '21 at 16:17
-
You could simply [Get the object that is causing `EventSystem.current.IsPointerOverGameObject` to be true](https://stackoverflow.com/questions/39150165/how-do-i-find-which-object-is-eventsystem-current-ispointerovergameobject-detect/39150616#39150616) and check if it is your background object – derHugo Oct 18 '21 at 08:53
1 Answers
0
To solve this issue I would add an Event Trigger component to your button then a button called add new will appear in the inspector and click it. After you clicked it then click Pointer Enter then that will detect when your cursor is over the button. After that, create a script named hoverDetect attached to your canvas with this code:
using UnityEngine;
public class hoverDetect : MonoBehaviour
{
static bool canJump = true;
public void cursorOver()
{
canJump = false;
}
}
in your if statement that detects whether you have the button pressed add some code that detects if canJump is true. Add some code that reverts canJump back to true if the cursorOver() is not called. Head back to event trigger, attach your canvas to the gameobject slot then select hoverDetect > cursorOver() and you are done!

Jaedon
- 3
- 2