I’m trying to fade out an instantiated prefabs color. Here’s what I have
public class ImageSpawner : MonoBehaviour
{
public Transform spawnTransformPos;
public GameObject spritePrefab;
public Sprite sprite;
public bool isSpriteSpawned;
void Start()
{
isSpriteSpawned = false;
if (spawnTransformPos == null)
{
spawnTransformPos = transform;
}
}
void Update()
{
if (Input.GetKeyDown("f"))
{
SpawnImage();
}
}
public void SpawnImage()
{
if (isSpriteSpawned == false)
{
isSpriteSpawned = true;
GameObject newSprite = (GameObject)Instantiate(spritePrefab, spawnTransformPos);
Color color = newSprite.GetComponent<SpriteRenderer>().material.color;
Debug.Log("Image spawned");
StartCoroutine(FadeSprite(color));
}
}
IEnumerator FadeSprite(Color _color)
{
while (_color.a > 0.0f)
{
_color.a -= 1.0f * Time.deltaTime;
yield return null;
}
}
}
Before cleaning it up a bit to paste here I had it verifying that _color.a was being reduced over time, however this _color was not attached to the Color of the instantiated prefab. Nothing would happen to the instance. What do I need to do to get the Color of the prefab and modify it?