- Home /
While mouse is pressed and moved to a button, button should be pressed as well
My problem: If I were to hold my mouse button down on a random area and then move it to a button, the button should be held down as well.
This is the code for when a button is pressed or held down:
public class downL : Button{
protected KnightControlScript knight;
void Start () {
knight = FindObjectOfType<KnightControlScript> ();
}
public void Update()
{
if(IsPressed())
{
WhilePressed();
}
}
public void WhilePressed()
{
knight.moving (-1);
}
}
This is the code for when I'm holding the mouse button down on a random position and moving it to a button:
public class dragLeft : MonoBehaviour, IPointerEnterHandler
{
private KnightControlScript knight;
void Start () {
knight = FindObjectOfType<KnightControlScript> ();
}
// Update is called once per frame
void Update ()
{
}
public void OnPointerEnter (PointerEventData eventData){
if(Input.GetMouseButton(0))
knight.moving (-1);
}
}
The problem with the code is that when I hold down my mouse button on a random position and moving it to a button, it only executes the button once, like a single press, even though my mouse button is held down, the button should be held down as well. Any help is appreciated!
Your answer
![](https://koobas.hobune.stream/wayback/20220611231931im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Get button under mouse 1 Answer
Can't interact with UI when using a virtual mouse, 0 Answers
Buttons and Event Triggers kill FPS 0 Answers
Click Goes Everywhere 0 Answers
How to rotate boject using UI button? 3 Answers