- Home /
Button doesnt work after reactivating Canvas.
Hello,
I'm creating a Tower Defense game where the player acts as a tower. The problem that I have is when a canvas is activated after some enemies getting to the end of the maze, which shows a "GAME OVER" text and a button that takes the player back to the main menu scene. But even though I have the scene loading script attached to the button and have EventSystem in place, the button doesn't seem to respond to the mouse press. Also at the same time, the cursor disappears and I need to click Esc every time after pressing the button to see the cursor again. I've tried to see if there is something inherently wrong with the button/canvas by creating a temporary scene and pasting it, but everything worked perfectly there.
Thanks in advance for any advice.
Is posible you have something in front of the button? another canvas or something?
One good thing to do, is use Raycast in a script and then Debug.log the hit.gameobject.name to know what object are you cliking... maybe will show you what is happening!
Good luck !
Thanks for the response. Will try, but honetly I don't know even how or why o what might be the cause.
It does sound like the "Game Over" text is left over the button making it un-clickable.
Answer by sdowmaster · May 08, 2019 at 07:47 PM
did you add the scene to the build settings?
Yes, I've added them. The problem is that after transitioning from the first-person camera to another camera( I use the second one for a player to be able to set position to which he can teleport) and then reactivate it along with the canvas where the game over is shown, the button doesn't even react to the mouse, and the mouse itself is not showing until I use Esc key, even though it's displayed for the first time.
use a debug code to see if it is pressed and while you start the game and this happens go check the button you are pressing, if it has the script loaded or it is "missing" as happens to me due to some mistakes i made. But you say you made another scene and it worked fine so i don't understand :) i am a little more than a beginner so i don't make sense xD
I'm also extremely confused. I started trying to deactivate objects bit by bit and see which may interfere, but the only time the canvas works are when I rewrite code a little bit so that the camera along with the canvas I reactivated right after deactivating. Which obviously got me nowhere.
Answer by darkStar27 · May 09, 2019 at 05:19 AM
Here attach this script to a GameObject in your hierarchy and click on the button.
It debugs the first thing that it hits on. The problem may have occured cause of something that is lying above your button.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class UIObjectDetector : MonoBehaviour
{
public static UIObjectDetector instance;
public EventSystem es;
public PointerEventData ped;
public List<RaycastResult> rr;
public bool debug;
void Awake ()
{
instance = this;
}
void Start ()
{
ped = new PointerEventData (null);
rr = new List<RaycastResult> ();
}
public void Update ()
{
if (Input.GetMouseButtonDown (0) && debug) {
IsOverUIObject ();
}
}
public bool IsOverUIObject ()
{
ped.position = Input.mousePosition;
rr.Clear ();
es.RaycastAll (ped, rr);
if (rr.Count != 0) {
Debug.Log("Object Clicked on " + rr[0].gameObject.name + " Parent is " + rr[0].gameObject.transform.parent.name);
if (rr [0].gameObject.layer == LayerMask.NameToLayer ("UI")) {
return true;
} else {
return false;
}
}
return false;
}
}
If that's not the problem, check if your button has Image attached to it and that its RaycastTarget property is checked.
Hope this Helps!
Thanks for the reply. Still nothing, but when I started looking at the event system I noticed that when I drag the mouse over the button, the pointerEnter property shows nothing, and when I to main menu scene and do the same, every button is shown in this property.
Answer by Elizabeth87 · May 09, 2019 at 11:47 AM
The problem is that after transitioning from the first-person camera to another camera( I use the second one for a player to be able to set position to which he can teleport) and then reactivate it along with the canvas where the game over is shown, the button doesn't even react to the mouse, and the mouse itself is not showing until I use Esc key, even though it's displayed for the first time. kodi
Answer by Er_Mol · May 09, 2019 at 08:23 PM
Okay, it started detecting the button when I pressed the Esc key when the camera on my player was still active(along with the entire player). When I did that, the mouse persisted to the next camera and the event system started recognizing stuff I was hovering over. Still, there is a problem of making the cursor active before the transition.