0

I have followed this tutorial to create a simple clock: http://catlikecoding.com/unity/tutorials/clock/

Everything works perfectly fine but the tutorial has an internal switch for digital and analog display. Now I would like to add a simple button "ingame" to switch that bool to true/false.

How can I achieve that ? thanks!

Exportforce
  • 87
  • 1
  • 3
  • 11

2 Answers2

3

Add a method to your script to change the value of analog.

public void OnClick()
{
   analog = !analog;
}

Create a button in your scene and make sure it's under a canvas, select the button, then drag your script into the OnClick() section. Select your OnClick() method from the dropdown list.

enter image description here

You an add multiple on click events from here, and this method allows you easily see what methods are attached to your buttons without looking at your code, which can get complex over time.

Steven Coull
  • 468
  • 3
  • 14
  • Much better solution than the accepted one. Using Unity's potential. – Dávid Florek Jun 08 '17 at 09:21
  • @DávidFlorek: When you will start to work on bigger projects with several people, believe me, you will quickly change your mind. ** insert but that's not my business image here** – Hellium Jun 08 '17 at 13:57
  • Don't worry, I already did. And it was such pleasure to let designers with very little understanding of code do their job and design. – Dávid Florek Jun 08 '17 at 13:58
  • In both cases, you have to deal with 3/5 lines of code. It's easier for a designer to let him drag & drop a prefab from the Assets, and drag & drop a single button instead of taking the risk he/she chooses the wrong method when dealing with the button component. – Hellium Jun 08 '17 at 14:01
0

Add a Button in your scene (under a canvas), and add the following code in your ClockAnimator script. Then, drag & drop the button gameobject inside the inspector of the script.

public UnityEngine.UI.Button button;

private void Start()
{
    // See https://docs.unity3d.com/ScriptReference/UI.Button-onClick.html
    if( button != null )
        button.onClick.AddListener( () => analog = !analog ) ;
}

You could achieve the same using a Toggle instead of a button :

public UnityEngine.UI.Toggle toggle;

private void Start()
{
    // See https://docs.unity3d.com/ScriptReference/UI.Toggle-onValueChanged.html
    if( toggle != null )
        toggle.onValueChanged.AddListener( ( isOn ) => analog = isOn ) ;
}
Hellium
  • 7,206
  • 2
  • 19
  • 49
  • Thanks! Works like a charm. Thought it would look a bit more easy. – Exportforce Jun 08 '17 at 08:47
  • Using a button's onclick property is good for changing its listeners at runtime, but I wouldn't use it to set up my button's listeners normally. If you're designing a UI, you may have many different buttons with more than one listener attached to each. Usually the listeners would be attached in the inspector, as I described in my answer. – Steven Coull Jun 08 '17 at 09:02
  • Using the Inspector is great for fast prototyping, but it's not robust, error prone (supposing your UI designer selects the wrong function to call) and is horrible when you work with prefabs. Moreover, it's easier to throw an error when you check the button is null instead of wondering why your button does not work because you haven't plugged the onClick in the inspector to your script. – Hellium Jun 08 '17 at 09:16