- Home /
Making button activate update with boolean
Hey so I have been searching for a long time and I'm sorry if someone already asked something similar. But I am trying to make my code work so that when I press my button is will active the update when true. In void start when I set timerOn = true; the code works but when I set it to false so that when the button is clicked and sets timerOn to true it doesn't work, but I know the value reached update because my Debug.Log shows the message (under the else if)...I hope what I am asking makes sense...please help I have been stuck for hours :(
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CountdownTimer : MonoBehaviour
{
[SerializeField] Text countdownText;
public AudioSource audioData;
public Button m_timer1;
private bool timerOn;
float minutes = 0;
float seconds = 3;
float miliseconds = 0;
void Start()
{
audioData = GetComponent<AudioSource>();
timerOn = false;
Button btn = m_timer1.GetComponent<Button>();
btn.onClick.AddListener(ClickBtn);
}
public void ClickBtn()
{
//Debug.Log("doesthiswork???!!");
timerOn = true;
Update();
}
public void Update()
{
if (timerOn == true)
{
if (minutes <= 0 && seconds <= 0 && miliseconds <= 0)// timerOn = false
{
timerOn = false;
//Debug.Log("CurrentTime = 0");
Music_Play();
}
else if (miliseconds <= 0)
{
Debug.Log("ithasreachedthisstage");
if (seconds <= 0)
{
minutes--;
seconds = 59;
}
else if (seconds >= 0)
{
seconds--;
}
miliseconds = 100;
}
miliseconds -= Time.deltaTime * 100;
countdownText.text = string.Format("{0}:{1}", minutes, seconds);
}
}
void Music_Play() //this works, now make the button work!!!
{
audioData.Play();
//Debug.Log("musicon");
}
}
Answer by mlnczk · Dec 03, 2018 at 02:36 PM
You can't call Update from other function! It calls itself every frame. Thats how it works. If you want to do somethingin update operate on booleans. So you button function just changes boolean on click and thats it. Just the way you do it just remove Update() from button function
@mlnczk oh thanks for the clarification! I have been really confused for too long on this. But I've removed calling Update() in the button function and it still doesn't work :/
Your answer
Follow this Question
Related Questions
UI button doesn't appear - c# 3 Answers
If statement breaking button color copy 0 Answers
Multiple Cars not working 1 Answer
Weird Boolean Problem c# 1 Answer
Can anyone help me in Bolt 0 Answers