foreach(SpriteRenderer sprite in sprites)
{
sprite.color = new Color(1, 1, 1, 1 - scale);
}
So this line of code is meant to change the alpha value of every single sprite in a GameObject’s script. However, when I tested it out, it just gives me a null reference exception.
Is this not how to change multiple alpha values of sprites, or am I just not doing said code right?
Again, it doesn’t matter. The answer is ALWAYS the same. ALWAYS.
How to fix a NullReferenceException error
https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/
Three steps to success:
- Identify what is null
- Identify why it is null
- Fix that
Ok, so i figured out what was the issue: I didn’t identify what “sprites” was.
sprites = transform.GetComponentsInChildren<SpriteRenderer>();
This should do it, though.