- Home /
Why Is yield return new WaitForSeconds() not working
Here Is My Script..
using UnityEngine;
using System.Collections;
public class SphereTextureChanger : MonoBehaviour
{
public Texture[] textures;
public int[] Fade_InTimes;
public int[] Wait_Times;
public int[] Fade_OutTimes;
private Material SphereMaterial;
void Start ()
{
SphereMaterial = GetComponent<MeshRenderer>().material;
if (Fade_InTimes.Length + Fade_OutTimes.Length + Wait_Times.Length + textures.Length == Fade_InTimes.Length * 4)
{
Debug.Log ("Animation Started");
for (int i = 0; i < textures.Length; i++)
{
SphereMaterial.mainTexture = textures [i];
StartCoroutine (Waiter (Wait_Times[i]));
FadeIn (Fade_InTimes[i]);
FadeOut (Fade_OutTimes[i]);
}
}
}
void Update ()
{
if (Fade_InTimes.Length + Fade_OutTimes.Length + Wait_Times.Length + textures.Length != Fade_InTimes.Length * 4)
{
Debug.LogError ("Number Of All Variables Are Not Equal");
}
}
void FadeIn(int duration)
{
for(int i = 0; i<=128; i++)
{
StartCoroutine (Waiter (duration/128));
SphereMaterial.SetColor("_TintColor", new Color(SphereMaterial.GetColor("_TintColor").r,SphereMaterial.GetColor("_TintColor").g,SphereMaterial.GetColor("_TintColor").b,i));
Debug.Log (SphereMaterial.GetColor("_TintColor").ToString());
}
}
void FadeOut(int duration)
{
for(int i = 128; i>=0; i--)
{
StartCoroutine (Waiter (duration/128));
SphereMaterial.SetColor("_TintColor", new Color(SphereMaterial.GetColor("_TintColor").r,SphereMaterial.GetColor("_TintColor").g,SphereMaterial.GetColor("_TintColor").b, i));
Debug.Log (SphereMaterial.GetColor("_TintColor").ToString());
}
}
IEnumerator Waiter(float delay)
{
yield return new WaitForSecondsRealtime (delay);
}
}
Everything is working Fine, But Only The Waiter() Is Not Working,It Is Not Delaying, Could Someone Tell Me My Mistake
Try debuging something inside your waiter coroutine, in this way you can get the idea what is actually happening to your script.
Answer by LeeroyLin · Jun 21, 2018 at 12:18 PM
A coroutine can only delay the things in that function. So, you can do this.
using UnityEngine;
using System.Collections;
public class SphereTextureChanger : MonoBehaviour
{
public Texture[] textures;
public int[] Fade_InTimes;
public int[] Wait_Times;
public int[] Fade_OutTimes;
private MeshRenderer sphereMeshRenderer;
void Start()
{
sphereMeshRenderer = GetComponent<MeshRenderer>();
if (Fade_InTimes.Length + Fade_OutTimes.Length + Wait_Times.Length + textures.Length == Fade_InTimes.Length * 4)
{
Debug.Log("Animation Started");
StartCoroutine(TextureChangerCor());
}
}
void Update()
{
if (Fade_InTimes.Length + Fade_OutTimes.Length + Wait_Times.Length + textures.Length != Fade_InTimes.Length * 4)
{
Debug.LogError("Number Of All Variables Are Not Equal");
}
}
IEnumerator TextureChangerCor()
{
for (int i = 0; i < textures.Length; i++)
{
sphereMeshRenderer.material.mainTexture = textures[i];
// Delay
yield return new WaitForSecondsRealtime(Wait_Times[i]);
yield return StartCoroutine(FadeInCor(Fade_InTimes[i]));
yield return StartCoroutine(FadeOutCor(Fade_OutTimes[i]));
}
}
IEnumerator FadeInCor(int duration)
{
for (int i = 0; i <= 128; i++)
{
Color c = sphereMeshRenderer.material.color;
c.a = i;
yield return new WaitForSecondsRealtime(duration / 128f);
sphereMeshRenderer.material.color = c;
Debug.Log(sphereMeshRenderer.material.color.ToString());
}
}
IEnumerator FadeOutCor(int duration)
{
for (int i = 128; i >= 0; i--)
{
Color c = sphereMeshRenderer.material.color;
c.a = i;
yield return new WaitForSecondsRealtime(duration / 128f);
sphereMeshRenderer.material.color = c;
Debug.Log(sphereMeshRenderer.material.color.ToString());
}
}
}
And you should notice that (int value / int value) always return an int value. By the way, you have not change the MeshRenderer's material, you change the ShpereMaterial, it's a new instance, So you need a MeshRenderer which is not a new instance. Maybe, this is what you want. Of course, if you want a transparent effect, you have to give sphere a shader which supported transparent effect.
Thank You So $$anonymous$$uch Sir, You $$anonymous$$ade $$anonymous$$y Day, I Can't Thank You Enough
Answer by kantagara · Jun 21, 2018 at 11:56 AM
Because you are passing duration as int, pass it as float, and it will work.
Sorry $$anonymous$$an, I tried Casting them "(float)" wherever i passed it, But its still not working