I am currently trying to get a very simple thing done, but looks like actually a pain since I didn’t find any solutions around.
I have a simple button, and I want to change a value on it ( the value is " Fill Amount", from 0 to 1 ) over time, like 1 second, once I clicked on it.
I have the script that change this value, but problem : I did it in the update() function, like so :
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
[RequireComponent(typeof(Button))]
public class eventButton : MonoBehaviour
{
public Button button;
void Start(){
button = GetComponent<Button>();
button.image.fillAmount = 0;
}
void Update () {
float timer = 1.0f;
float timerTrack = 0;
while (timerTrack < timer)
{
timerTrack += Time.deltaTime;
Debug.Log(timerTrack);
button.image.fillAmount = timerTrack;
}
}
}
And when I am trying to link my script to the “OnClick()” function on my button, I can’t actually “launch” the script, it is only accepting a function inside my script.
So my question is : Is there a way to call directly the update() function of a script, and not a specific function ?
Filling the image would happen during one frame in your script. You need to do that over time, not instantly. This can be achieved with coroutines, for example.
‘Button’ has no field or property called ‘image’. You should have received some errors there.
I’ve created a small example that does what you orignally expected from the script above.
I used a coroutine for that.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class EventButton : MonoBehaviour
{
public Image image;
private bool isFading = false;
public void FadeIn()
{
if(!isFading)
StartCoroutine(StartFadeIn());
}
private IEnumerator StartFadeIn()
{
isFading = true;
image.fillAmount = 0;
while (image.fillAmount < 1)
{
image.fillAmount += Time.deltaTime;
yield return null;
}
isFading = false;
}
}
*Edit
Please note that I’ve changed the class’ name from ‘eventButton’ to ‘EventButton’. Rename the script accordingly.