Unity 5.4 Button auto-clicks - c#

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

Related

If and ActiveSelf. Only works once. With example project

First time developer here and I am struggling with a problem.
I made a mock up of the problem in regular Unity 3d since the VR scares some talented people away from helping me. That can be downloaded here: https://gofile.io/d/qHDlUZ
File is a zip called: SoundTestTroubleShooting. A Unity build on 24.7MB
Follow the number on the buttons.
Button 1 "Choose Music" Activates the Cube.
Button 2/4 "Play Music" Checks if cube is activated and plays song if that is the case.
Button 3 "Restart Scene" Stops the music and reloads the scene.
Cube has dontdestroyonload.
After that i press button 2/4"PlayMusic" and it cant find that the cube is active.
WHY?! Going nuts here.
Old text with the entire problem here:
In my game I have four songs in the start menu that my player gets to choose from. He chooses one by pressing one of four buttons. This activates an empty GameObject, lets call it SmileMusicTrigger. SmileMusicTrigger has a dontdestroyonload and is deactivated by the other three buttons if player wants to switch song.
Player press Begin which moves him into position. He presses start and this button has:
public GameObject SmileMusicTrigger;
public GameObject EpicMusicTrigger;
public GameObject GodMusicTrigger;
public GameObject VikingMusicTrigger;
void OnTriggerEnter()
{
if (SmileMusicTrigger.activeSelf)
{
AudioManager.instance.Play("Smile");
}
if (EpicMusicTrigger.activeSelf)
{
AudioManager.instance.Play("Epic");
}
if (GodMusicTrigger.activeSelf)
{
AudioManager.instance.Play("God");
}
if (VikingMusicTrigger.activeSelf)
{
AudioManager.instance.Play("Viking");
and my game start and and the music begins fine.
When player dies he restarts the scene with restart button which has this to say about the music:
void OnTriggerEnter()
{
AudioManager.instance.Stop("Smile");
AudioManager.instance.Stop("Viking");
AudioManager.instance.Stop("God");
AudioManager.instance.Stop("Epic");
}
}
after this code has run the scene restarts and it does not seem like my Start button can check the active state of SmileMusicTrigger again. I know SmileMusicTrigger is active through the restart of the scene because I see it under dontdestroy in the hierarchy when game is running and it is checked as on in inspector. But I cant seem to call it again. Thus my music only plays once.
Audiomanager is also under Donotdestroy so I dont think the problem is there.
Sorry if this is messy. I am working really hard to get this working but I am so stuck right now.
Thankful for any help.
All the best!

Can not add click event on gameObject Unity

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.

How to load prefab from folder only when player want?

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.

Unity: Add Button Script to 3D gameObject

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.

unity 5 button not showing up in editor

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.

Categories