I am trying to Instantiate a enemy prefab at random intervals. The enemy should fade in gradually instead of just appearing.
This is what I have so far:
using UnityEngine;
using System.Collections;
public class SpawnEnemy : MonoBehaviour
{
public GameObject prefab;
public GameObject[] spawnPoints;
public float minTime = 5.0f;
public float maxTime = 10.0f;
public float fadeInDuration = 3.0f;
// Use this for initialization
void Start()
{
Invoke("Spawn", Random.Range(minTime, maxTime));
}
void Spawn()
{
GameObject instance = (GameObject)Instantiate(prefab, spawnPoints[Random.Range(0, spawnPoints.Length)].transform.position, Quaternion.identity);
StartCoroutine(FadeIn(instance.GetComponentInChildren<SkinnedMeshRenderer>().material.color));
Invoke("Spawn", Random.Range(minTime, maxTime));
}
IEnumerator FadeIn(Color instanceColor) {
for (float t = 0.0f; t < fadeInDuration; t += Time.deltaTime)
{
instanceColor = Color.Lerp(new Color(1.0f, 1.0f, 1.0f, 0.0f), new Color(1.0f, 1.0f, 1.0f, 1.0f), t / fadeInDuration);
yield return instanceColor;
}
}
}
However, the enemy just appears instantly as the spawn point with no noticable fade in. What I am doing wrong? I feel like I’m missing something obvious, but all I could find for answers relating to this question used the Update() function.