0

I am making a mobile game in Unity, and I want to make some text fade out. I have created a for loop that increases the alpha value of the text's color by one 255 times. When I run the code, it gives me a NullReferenceException, saying "Object reference not set to an instance of an object". Here is the for loop causing the problem.

   {
       for(int alphaVal = 0; alphaVal < 256; alphaVal++)
       {
           text.color = new Color(red, green, blue, alphaVal);
       }
   }


haldo
  • 8,119
  • 5
  • 24
  • 35

1 Answers1

1

Your text variable is not assigned. Make sure you drag it in through the inspector. Also, unitys color object does not accept 255 values. It accepts a float ranging from 0 to 1. Passing in anything higher than 1 will always result in a value of 1 (full alpha)

Immorality
  • 2,083
  • 2
  • 8
  • 22