First time I try to use the new UI, and have two problems !
1st :
When I click on a button, the function OnClick work as well but if I hit spacebar, it’s like I click again on the button and the function restart …
2nd :
I have some objects behind UI who use raycast … My game is a RTS and ground is selectable.
When I click on UI, clicks affect objects behind too !
If you have any tips, it’s welcome !
Thanks Comm !
These are guesses, as I haven’t run into these problems myself:
1st:
Check Edit → Project settings → Input and see if “spacebar” is one of your submit buttons. (check positive and alt positive… you’ll know what I mean when you’re in the menu)
2nd:
put some additional colliders on your UI that your raycast will hit (make some new gameobjects if you need to). When you do your raycast, check if you hit any of the UI colliders, and ignore your usual raycast logic if you do.
Yes “sapcebar” is used in my game, i set it for turns pass, so it’s an important button, but why this one click and not others inputs setted ?
Oh great ! I never thought that I can place colliders on UIs objects …! Just tryied but it’s like my raycast pass trought them …
EDIT:
Just saw when I use arrow keys (camera moving in my game), changing the “nears buttons” I click on HoverState … And then if I use spacebar, it’s click on the button hovered by arrows.
(Hard to explain)
If your raycast is passing through your UI colliders, make sure you’re not ignoring the UI layer in your raycast. If the UI is rendered by a different camera than the rest of your game, you’ll need to raycast from both cameras. (I recommend raycasting from your UI camera first, so if you find a hit there, you don’t have to raycast from your game camera)
To make sure the “submit” button (your spacebar, in this case) doesn’t activate buttons, change the “navigation” dropdown to “none” in the inspector with the button selected.
Ah thanks a lot for the “none” advice for UIbuttons !!
I’m not ignoring the raycast (UI/ignoreRaycast disable in physics) and I render it on the basic main camera of the scene but still not working … I put BoxColliders and SphereColliders on them, that’s good ? Don’t need 2D colliders ?