Awake alternative for UI button (On-Off)
Hi Everyone,
I am new to Unity and coding, and need a little help with this script. I have this on a button which works fine if the button is set to Active but it only works once. I could have another button to switch it off but would prefer the same button to toggle it on and off.
Any help would be massively appreciated.
Thanks.
public bool ActivateInAwake = false;
public bool Active { get; set;}
void Awake()
{
timer = GetComponent<CustomTimer>();
if (ActivateInAwake)
Active = true;
else
Active = false;
}
Are you trying to make the button perform this private Awake() function when clicked?
Thanks for the reply, that's correct. Currently I have this script attached to a game object which is connected to my button. It works fine, but only once. I need a solution where you click the button, it starts, click it again to stop and so on.
Thanks
Aaron
Answer by CoryButler · Jul 11, 2017 at 12:34 AM
A Button component's onClick event cannot call a private function. Instead, try having it call something like this:
public void ToggleActive()
{
// If Active is true, make it false, and vice versa.
Active = !Active;
}
Do you know how to add a function call to a Button's onClick event in the Inspector?
Hi,
I think so, I amended the script, added the button which I then switched from active to toggle and it works I think... The toggling.
The only issue is I'm not sure the whole script is working. I press once the health discharges, toggle again it carries on discharging. :-(
Basically I'm following a Pet Tutorial.
Really appreciate the help BTW. Thanks
using UnityEngine;
using System.Collections;
// this class will restore ceratain care value on our character with time.
// for example, Sleep should be restored when the character is sleeping.
public class CareTimedRefill : $$anonymous$$onoBehaviour {
public bool ActivateInAwake = false;
public bool Active { get; set;}
public Care CareToRefill;
public float IncrementTime;
private CustomTimer timer;
void Awake()
{
timer = GetComponent<CustomTimer>();
if (ActivateInAwake)
Active = true;
else
Active = false;
}
void Start()
{
//Debug.Log(timer.gameObject.name);
timer.Interval = IncrementTime;
timer.Elapsed += TimeElapsed;
}
private void TimeElapsed()
{
if (Active)
CareToRefill.AddCare();
}
}
I do not see any errors as long as you are setting the IncrementTime variable in the Inspector to something other than 0. I assume the GameObject you are attaching this script to also has a CustomTimer component.
Awake() is a $$anonymous$$onoBehaviour function that is only called once, and it is called before Start(). Since ActiveInAwake is set to false, Active will always be set to false in the Awake() function. If you are not going to use a default getter and setter on Active, you could simplify you code to something like this:
using UnityEngine;
using System.Collections;
public class CareTimedRefill : $$anonymous$$onoBehaviour {
public bool Activate = false;
public Care CareToRefill;
public float IncrementTime;
private CustomTimer timer;
void Start()
{
timer = GetComponent<CustomTimer>();
timer.Interval = IncrementTime;
timer.Elapsed += TimeElapsed;
}
private void TimeElapsed()
{
if (Active)
CareToRefill.AddCare();
}
}
Are you receiving any error messages in the console when you run the game?
Brilliant, Thanks Cory. The previous solution worked. Whats odd though is I needed to change the custom timer from private to public and then add the script outside via a GameObject. Anyway seems to work now.
Again Thanks
Your answer
Follow this Question
Related Questions
How can I make a continue button with the following scripts 0 Answers
How to make ui only activate once 1 Answer
Move gameObject on UI Button Press 2 Answers
Unity UI OnClick AddListener 1 Answer