- Home /
Im new in unity and c# - i tried to make a image fill amount decrease.
I do not know what I'm doing wrong , here is my code , if anyone can help me , I thank.
public float waittime 10.0f;
public Void Tocar() goAudioSource.Play ();
while (goAudioSource.isPlaying){
tempo.GetComponent<Image>().fillAmount -= 1.0f/waittime * Time.deltaTime;
if(tempo.GetComponent<Image>().fillAmount == 0.0f){
goAudioSource.Stop ();
}
}
Answer by _Yash_ · Jul 15, 2016 at 05:42 AM
while (goAudioSource.isPlaying){
tempo.GetComponent<Image>().fillAmount -= 1.0f/waittime * Time.deltaTime;
if(tempo.GetComponent<Image>().fillAmount == 0.0f){
goAudioSource.Stop ();
}
here you are decreasing to 0 in single Update cycle. what you want is animating effect.
for that change while with if so that in every frame it will decrease a little.
Answer by daniloescobar · Jul 16, 2016 at 04:29 PM
It does not work right . It decreases only once. Here is my complete code with the if .
public class microfone : MonoBehaviour {
public int contador;
public int cont;
public string bot;
public float waittime = 1.0f;
private bool micConnected = false;
Text tocarbotao;
private int minFreq;
private int maxFreq;
void Start()
{
contador = PlayerPrefs.GetInt ("verdade"); //Contador inicialmente recebe o valor de numero de perguntas cont=contador; // O cont vai ser igual ao contador PlayerPrefs.SetInt("contador",cont);
tempo.GetComponent<Image>().fillAmount = 0;
if(Microphone.devices.Length <= 0)
{
Debug.LogWarning("Sem Microfone!");
}
else
{
micConnected = true;
Microphone.GetDeviceCaps(null, out minFreq, out maxFreq);
if(minFreq == 0 && maxFreq == 0)
{
maxFreq = 44100;
}
goAudioSource = this.GetComponent<AudioSource>();
}
}
public void Tocar()
{
tocarbotao = gameObject.GetComponent<Text> ();
bot = tocarbotao.text;
if (cont > 0){
if (bot == "GRAVAR") {
if (micConnected) {
if (!Microphone.IsRecording (null)) {
goAudioSource.clip = Microphone.Start (null, true, 20, maxFreq);
gameObject.GetComponent<Text> ().text = "ANALISAR";
}
}
} else {
gameObject.GetComponent<Text> ().text = "GRAVAR";
Microphone.End (null);
goAudioSource.Play ();
if(goAudioSource.isPlaying){
tempo.GetComponent<Image>().fillAmount -= 1.0f/waittime * Time.deltaTime;
if(tempo.GetComponent<Image>().fillAmount == 0.0f){
goAudioSource.Stop ();
gameObject.GetComponent<Text> ().text = "GRAVAR";
}
}
cont=cont-1;
}
if (cont == 0){
gameObject.GetComponent<Text> ().text = "FIM";
}