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.
Related
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.
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).
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
I make menu for my game (Android, C#) and i have simple problem. I want change image in button when the button is pressed. What is the best way currently? Not some preset way in Unity? Thanks
Use button animations. They can be auto generated. Change the sprite in the button press animation manually after generation.
Cost efficient way would be changing sprite object sprite field by getting the UI Image component in the button object from script.
I'm trying to create a start menu for my unity 5.3.1 game by following this tutorial : https://www.youtube.com/watch?v=pT4uca2bSgc
I followed everything he said up to the part when he attached the script to the start button on the main menu screen. Here is a picture of my code and editor:
Any help is appreciated thanks.
You have attached that Title script to the "canvas" Canvas Game Object - not the "start1" Button Game Object.
In reality in doesn't matter, because you can tell your button to call any method on any game object when clicked.
To do that, select your button, for the OnClick event drag the Canvas game object, then from the drop down select the Title script and the startLevel() method.
Now when you click the button your level will load.