I'm trying to get a game object in Unity 3D to fade out(fade speed should be adjustable from editor), pause/wait 2 seconds (pause length be adjustable from editor), and fade back in, looping infinitely. Coroutine is what I'm trying to utilize here to decrease alpha value but I'm unsure of exactly where I'm making my errors.
I'm getting a single error. (Cannot convert method group "FadeOut" to non-delegate type "object")
Here is my code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScriptFader : MonoBehaviour
{
// attached game object for fading
public GameObject Sphere;
// fade speed length
public float fadeSpeed;
//Pause length between fades
public int fadePause;
void Awake()
{
StartCoroutine(FadeOut(fadeSpeed));
}
//Fade Out Coroutine
public IEnumerator FadeOut(float fadeSpeed)
{
Renderer rend = Sphere.transform.GetComponent<Renderer>();
Color matColor = rend.material.color;
float alphaValue = rend.material.color.a;
//while loop to deincrement Alpha value until object is invisible
while (rend.material.color.a > 0f)
{
alphaValue -= Time.deltaTime / fadeSpeed;
rend.material.color = new Color(matColor.r, matColor.g, matColor.b, alphaValue);
yield return new WaitForSeconds(fadePause);
}
rend.material.color = new Color(matColor.r, matColor.g, matColor.b, 0f);
StartCoroutine(FadeIn(fadeSpeed));
}
//Fade In Coroutine
public IEnumerator FadeIn(float fadeSpeed)
{
//waits for the return value of FadeOut coroutine to commence
yield return FadeOut;
Renderer rend = Sphere.transform.GetComponent<Renderer>();
Color matColor = rend.material.color;
float alphaValue = rend.material.color.a;
//while loop to increment object Alpha value until object is opaque
while(rend.material.color.a < 1f)
{
alphaValue += Time.deltaTime / fadeSpeed;
rend.material.color = new Color(matColor.r, matColor.g, matColor.b, alphaValue);
yield return null;
}
rend.material.color = new Color(matColor.r, matColor.g, matColor.b, 1f);
StartCoroutine(FadeOut(fadeSpeed));
}
}