- Home /
NullReferenceException appears when fading a GUITexture
I am using the Fade function in http://wiki.unity3d.com/index.php?title=Fade#Examples
But when I click play, NullReferenceException appears.
Here is my code:
var startText: GUITexture;
function Start () {
startText.enabled = false;
}
function Update () {
if ( PlayMovie.Played == 1 )
{
startText.enabled = true;
Fade.use.Alpha(startText, 0.0, 1.0, 3.0, EaseType.In);
}
}
What should I do?
Answer by robertbu · Jan 09, 2014 at 06:50 PM
This script assumes you've initialized 'startText' in the Inspector through drag and drop. If this scrip is attached to the GUITexture object, you can add the following to the top of the Start() function to fix the problem:
if (startTexture == null)
startTexture = guitexture;
This initializes 'startTexture' to the guiTexture attached to the current game object.
Or you can just drag and drop the game object that has the GUITexture onto the 'startText' variable.
Do you mean this? http://answers.unity3d.com/storage/temp/20523-scrn.png
I have already attached the gui texture to the inspector. But NullReferenceException still appears.
NullReferenceException: Object reference not set to an instance of an object
FadeOutText.Update () (at Assets/FadeOutText.js:17)
This is the message I receive from the console. And this is line 17:
Fade.use.Alpha(startText, 0.0, 1.0, 3.0, EaseType.In);
It works now. I just forgot to create the game object and attach the fade script to it. Thanks.