2D Toolkit Forum
2D Toolkit => Support => Topic started by: ItsMeAlan on March 09, 2013, 12:27:42 am
-
Is there a way to change how transparent a sprite is with code?
Thanks!
-
Yup, change the alpha channel of the color property.
sprite.color = new Color (1, 1, 1, 0.5f ); // makes it 50% transparent
-
I'm using this:
GetComponent(tk2dSprite).color = new Color(1,1,1,myAlpha);
where myAlpha is a float that I add to or decrease from. Interestingly, this code works exactly as I want it to, but it causes a ton of errors. Specifically, this error:
NullReferenceException: Object reference not set to an instance of an object
HelpArrowScript.Update () (at Assets/Scripts/HelpArrowScript.js:23)
The line causing the error is the line I posted above. My game is having major framerate issues, so I figure this might be the culprit? Am I doing something wrong?
-
In JS, the syntax is
GetComponent(tk2dSprite).color = Color(1, 1, 1, myAlpha);
-
I took out the New but this line is still causing the same error once per frame.
-
What is GetComponent(tk2dSprite) returning? Do you have a sprite attached to the same gameobject the script is on?
-
Ah, that was it. I'd accidentally applied my script to an empty gameObject without realizing it. Thanks!