Hey awesome people.
So I have made a UI panel with 6 buttons in it, now I want to do stuf by pressing and holding down a button in this panel.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class UIController : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
// Create array of buttons to place the buttons for movement in.
// Serialize for easy access in the editor.
[SerializeField]
private Button[] moveButtons;
// Create array of buttons to place the buttons for rotating the Pen/Player inside.
// Serialize for easy access in the editor.
[SerializeField]
private Button[] rotateButtons;
private bool isPressed;
// Start is called before the first frame update
void Start()
{
isPressed = false;
}
// Update is called once per frame
void Update()
{
if(isPressed != true)
{
Debug.Log("Button is NOT pressed");
}
else if(isPressed == true)
{
Debug.Log("Button IS pressed");
}
}
public void OnPointerDown(PointerEventData eventData)
{
isPressed = true;
}
public void OnPointerUp(PointerEventData eventData)
{
isPressed = false;
}
}
Ie when I press a button it’s supposed to output “Button IS pressed” but all I get in the console i “Button is NOT pressed” thus is seems it does not register a button pressed (see picture)
please help.
You have 6 buttons, and all of them are going to be outputting a log message every frame. Your screenshot only shows the last 5 messages. So even if your first button was outputting something different, you wouldn’t see it in that screenshot.
And of course, if you released the button for a second so that you could pause the game and then examine the console more carefully, it will have rapidly filled up with “not pressed” messages in the split second between when you released the button and when you paused the game.
Try clicking “collapse” in the console window to group together all identical messages.
Alternately, try this with just one button in the scene and make sure that works before you try to do multiple buttons at once.
I tried hitting collapse but that does not change anything, that is no “button IS pressed” messages in the console, so hitting collapse only outputs “button is NOT pressed”
Ie; it seems that it does not recognise that a button is pressed.
OK, well, your PointerDownHandler implementation looks fine, so I’m guessing your problem will be something to do with the way your objects are set up. Some possibilities include:
- Your UIController script is attached to the wrong thing
- Your UIController object doesn’t have anything that counts as a valid raycast target (and therefore it doesn’t detect clicks)
- Some other raycast target is “in front” of your UIController and blocking the clicks
- Some child of your UIController is handling the clicks rather than propagating them up the hierarchy (for example, a Button or EventTrigger component could do this)
If you’re not sure what your pointer might be “clicking”, try selecting your EventSystem in the hierarchy while your game is in play mode; this will display various information that might be helpful for debugging.