- Home /
Change the alpha of SpriteRenderer with JavaScript in Unity 4.3
Hello everyone. My name is Tino and I'm new in the Unity Answers section. Over the last year or so I have usually been able to find the answer to my questions here, until today.
Over the last couple of days I have been experimenting with the new 2D features of Unity 4.3. Today, I tried to change the alpha value of the Sprite Renderer with JavaScript but I couldn't get it to work. I have tried various approaches but couldn't get it to work. The code I'm struggling with can be found below.
The script is attached to a gameobject with the components Transform, Sprite Renderer, and Polygon Collider 2D.
Hopefully, someone here is able to correct my error because this seemingly simple problem is taking much longer than anticipated.
Cordially,
~Tino
// Works like a charm
function OnMouseOver () {
this.renderer.material.color.a = 1.0;
}
// Throws an error
function OnMouseOver () {
this.renderer.spriteRenderer.color.a = 1.0;
}
Answer by Eric5h5 · Nov 28, 2013 at 06:54 PM
GetComponent(SpriteRenderer).color.a = 1.0;
Your answer
Follow this Question
Related Questions
How to access SpriteRenderer's Mesh 1 Answer
How change sprite animation texture?? 2 Answers
Gui.Box - Sprite 0 Answers
Displaying Prefab Sprite as Texture on GUI Elements 0 Answers