- Home /
Button on Panel is not responding
I have a Panel added into a Canvas. I am using this Panel as showing popup/dialog to user. In the design mode this Panel is not position into the game view and kept aside (away from Camera viewport). When I need to show this Panel I position it in the center of the screen using following script:
GameObject popup = GameObject.Find("Popup"); popup.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width / 2, Screen.height / 2, Camera.main.nearClipPlane));
It does shows in the middle of screen but Button placed on this Panel is non clickable or does not respond to click. I have attached debugger to this button OnClick method and it does not hit.
When I keep this panel all the time in game view, this button is clickable and execute OnClick method.
Canvas's Render Mode is set to "Screen Space - Camera"
Please refer the attached image for more detail.
Is there anything in popup children that could block the raycast to the button? Can you please make a screenshot with the popup gameobject expanded?
Have you already tried another method to activate the popup? Maybe try this:
public GameObject popup; // store object to avoid any issue
public void setPopup(bool set){ //button trigger
popup.SetActive(set);
}
Thanks @DevManuel, I have already tried your suggestion and it works if I keep popup panel in the scene view or inside Canvas. But to declutter objects at design time I am keeping panel aside (outside Canvas) and at runtime bring back popup Panel to game view, whenever there is need to show the popup. So as a matter of fact I am not even deactivating it. It is active all the time but because it is outside Canvas, Unity runtime does not show/render it on active Game view.
Answer by OblicaStudio · Jun 08, 2021 at 10:34 AM
@Earthshine, I dont think so. I have added screen shots of Popup structure. This screen shot also contains Inspector values of Popup Panel and Button.
I my understanding, I have put Popup Panel out of Canvas rendering area at design time (Scene View) but I am pulling Popup Panel inside Canvas via changing its position to middle of screen and that may causing the issue. My Canvas's Render Mode is set to "Screen Space - Camera" so at design time it is covering Main Camera view port, its not like the huge default Canvas size I have here.
Everything looks ok in your screenshots as far as I can see.
I actually don't know if popup.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width / 2, Screen.height / 2, Camera.main.nearClipPlane));
should give you the right position on the Canvas and I don't know if button can be clicked if its outside the canvas plane. AFAIK you are supposed to use RectTransform for moving and placing objects in the UI.
I wouldn't even take this route however, because I see no reason for moving the popup. I would instead write a script that disables the popup when the game starts and enables it again when the level ends, so I may leave it disabled or enabled in the edit mode.
Bingo!!!. Thanks a lot for pointing me into right direction. For UI objects, changing transform.position does not work. So as mentioned by you, I have used RectTransform and it worked.
popup.GetComponent().anchoredPosition = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width / 2, Screen.height / 2));
I understand this may not be the right approach (keeping panel outside Canvas at design time and bring it back on Canvas at runtime) but it greatly declutter my objects space at design time.
Thanks again!!