I want to add a click event on my gameObject, but it won't work, I don't know why it don't work on gameObject, but it work on the test button. I don't know why. Can anybody help me with it.
This is the click code:
public void cardsClick()
{
Debug.Log("aa");
}
And this is how I add click event to game object
Buttons Script
I'm not sure where you add the button script. However make sure you add it to Buttton not to Canvas.
Activate Buttons
If you add it correctly I can guess you make this cards inactive before. Then you turn it to active. In this situation Unity is bugged and buttons stop work. Try Button.Interactable instead.
Target graphics
Also if I'm not right try to add target Graphics, because Unity can don't know where is the button.
I don't have more ideas what could went wrong, but I hope I help you.
Related
I have got some 2D elements in my scene with CircleCollider2D on them and a script with OnMouseUp(). At some stage a UI panel is rendered over the objects, and when I press a button that happens to be above the element with the OnMouseUp() it triggers both. I want to avoid disabling the circle collider every time the panel shows up and then enabling it after because of some other functionality. What can I do? Do I have to use raycasting?
Thanks for all help in advance.
The OnMouseButton() does not answer objects in the Ignore raycast layer, so change the layer between those will solve your problem, another approach is to make an event when you open and close your menu and a bool field in your character like IsClickable, then you just toggle it as the answer to UI open/close event. Hope this helps
You can check if the cursor is over UI. You can do this with the EventSystem.IsPointerOverGameObject() method, which returns a boolean. The documentation for the method.
In my scene, I have this set-up:
Game Controller
Level Controller
Main Camera
Event System
Canvas
Player
PlayerCanvas (Render Mode is World Space)
Button
I set the PlayerCanvas into the same width and height of the button. And both of them are just small. I put Debug.Log to check everytime I press the button. But somehow, it triggers the button even if I click way off the screen. Can help to explain why is this happening. Thanks!
Note: I'm trying to add a button beside the Player so that even if the Player moves, the button will just follow.
I think your hierarchy is wrong.
Try something like
Game Controller
Level Controller
Main Camera
Player
Event System
Canvas (Screen Space - Overlay)
Button
Not sure what Player is, but I'm sure on my hierarchy Event System and Canvas are on the same level. And every button is son of Canvas. Not sure you can make multiple Cavas (you should not need it anyway).
I have a many prefabs in game but I want to load them only when player click button not on start runtime. I know there is only way to load prefabs from Resources folder but it gives me the problem the prefabs are loading all in same time at runtime start not when player click the button.
You can add the same function on OnClick event.
Click on Button component. there would be an OnClick event.
Click on +. Drag the gameobject which contains the method and drag that method on OnClickEvent.
Let me know if it helps.
I'm trying to add the Unity Button Script to a 3d gameObject. When I tried this by adding it onto a cube, I can't get the desired behaviours. How can I get the 3d object to work like a button? In particular the highlighting colors, the click events. I was speculating that perhaps it uses the wrong raycaster, and if the raycaster specificaiton is changed some how it would work? Does anyone have a good enough understanding of the source code to explain if this is possible, and if so what I would need to do to achieve this? Thanks.
Rik
You could do it by using OnMouseDown() function. Here is the link Unity Docs
You will also need to add the image script along with the button script.
When I create a button in unity3d 5.4 the button just clicks automatic when i press play in the editor. I haven't been using unity before, so I can't say if it would happen in other versions.
First i create a script, then I attatch it to a empty gameobject. Then I press the little + sign on the OnClick() in the button properties, so I can add the gameobject with the script attached.
I have screenshots of button properties, eventSystem, Canvas, gameobject and the Script.
If anyone know what i have done wrong, please let me know. Thanks in advance.
The screenshots are in this post, since I cant post 5 images in stackoverflow:
http://forum.unity3d.com/threads/unity-5-4-button-auto-clicks.426526/
When you press play in editor, Button is NOT pressed at all. Its just that you have Debug.Log("Clicked!") in Start() method. Start() is called by Unity automatically when you run the application. It has nothing to do with button click. You need to register OnClick() listener method so it will be called when you press the button.
Have a look at this tutorial : https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-button
EDIT :
Please learn more about scripting here: https://unity3d.com/learn/tutorials/topics/scripting