Question by
soccer1mt · Aug 20, 2016 at 01:32 PM ·
rendererlistschildrenfadeoutrenderer.material.color
Issue where Fading out Game Object causes screen to blank and then reload with gameobject gone
The objective is to have a "smoke" to fade away to nothing after 2 seconds of being instantiated. When it is instantiated, the game (in VR) blanks out to the blank white grid screen and then the screen reloads after two seconds with the smoke gameobject gone.
No clue as to why this is happening. Is this function too resource intensive?
using UnityEngine;
using System.Collections;
public class TimeForSmoke : MonoBehaviour {
public Renderer[] smokeFades;
private float timeLeft=2.0f;
// Update is called once per frame
void Start()
{
smokeFades = GetComponentsInChildren<Renderer>();
}
void Update ()
{
StartCoroutine(FadeOut());
timeLeft -= Time.deltaTime;
if (timeLeft < 0)
{
Destroy(this.gameObject, 0f); //removes instantiated smoke
}
}
IEnumerator FadeOut()
{
for (float f = timeLeft; f >= 0; f -= 0.1f)
{
Debug.Log("1");
foreach (Renderer smokeFade in smokeFades)
{
Debug.Log("2");
Color opacity = smokeFade.material.color;
opacity.a = f;
smokeFade.material.color = opacity;
}
Debug.Log("3");
yield return new WaitForSeconds(.1f);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
FadeIn/Out object composed of multiple sprites? 2 Answers
How to create a list in a specific order of one script that is used in different gameobjects? 0 Answers
Edit sharedmaterial in runtime but without override the material asset 1 Answer
Help with script , Change tag depending on material attached 1 Answer
2 variables holding Renderer.materials change at the same time... 0 Answers