Hi,
I’m sure this should be straight forward, but I’m new to C# and unity.
I can fade out a single object no problem using:
myRenderer = GetComponent();
and
myRenderer.material.color = Color.Lerp(fromColor, toColor, t / fadeTime);
(full code below.)
But I can’t do this for grouped objects. I have a lot of objects, also with nested groups and I can’t figure out how to best cycle through to fade out all the items.
I would have thought I’d need to get myRedender into an array, but GetComponent doesn’t like that.
I have tried Fade out children of gameobject - Questions & Answers - Unity Discussions
But this only seems to change the colour not the alpha.
Any help on this would be greatly appreciated.
Thanks in advance for any guidance.
public class Muscles : MonoBehaviour {
public float fadeTime = 1f;
Color solidColor;
Color fadedColor;
Color currentColor;
bool fading;
bool faded;
Renderer myRenderer;
// Use this for initialization
void Start()
{
myRenderer = GetComponent<Renderer>();
solidColor = myRenderer.material.color;
fadedColor = new Color(solidColor.r, solidColor.g, solidColor.b, 0f);
}
public void startFading()
{
// go = true;
Debug.Log("startFading");
if (!fading)
{
StartCoroutine(Fade());
}
}
IEnumerator Fade()
{
Debug.Log("coruntine started for muscles");
fading = true;
Color fromColor = faded ? fadedColor : solidColor;
Color toColor = faded ? solidColor : fadedColor;
for (var t = 0f; t < fadeTime; t += Time.deltaTime)
{
Debug.Log("fading muscles");
//currentColor = Color.Lerp(fromColor, toColor, t / fadeTime);
myRenderer.material.color = Color.Lerp(fromColor, toColor, t / fadeTime);
yield return null;
}
fading = false;
faded = !faded;
}
}