- Home /
Problem with GUITexture.color.a
Hi. Look at this snippet:
GameObject.Find("GUI Texture Pause").guiTexture.color = Color.black;
GameObject.Find("GUI Texture Pause").guiTexture.color.a = 0.4f;
First line of code works but second doesn't. I get this message:
Assets/Standard Assets/Scripts/Pause.cs(11,65): error CS1612: Cannot modify a value type return value of `UnityEngine.GUITexture.color'. Consider storing the value in a temporary variable
Help me who can please!
Judging from your error message it sounds like you need to create a Game Object for the texture.
Example:
public GameObject GUITexturePause;
void Start()
{
GUITexturePause = GameObject.Find("GUI Texture Pause");
}
I'm wondering why you would want to change the color at run time it might be best to just have 2 textures if you only have 2 colors, but either way should work fine assu$$anonymous$$g your code works once you set up the game object variable like my example.
I've never personally tried to change colors of textures like that so I'm not 100% sure, I only change colors for fonts.
Perhaps change the colour or the Texture2D associated with the GUITexture ins$$anonymous$$d.
Answer by robertbu · Oct 22, 2013 at 10:11 PM
The error message means you need to put it in a temporary variable and then put it back:
Color colorT = guiTexture.color;
colorT.a = 0.4f;
guiTexture.color = colorT;
Oh, Thank you, guys! Especially robertbu. Your idea really work. And, well, it works when picture is colored! But, if GUITexture has color.a property, why it's imposible to change it directly?
It's because the color is passed by value. Sort of like passing an int
to a function: changes made inside the function don't normally alter the value of the variable that was passed in.
guiTexture.color
gives you a copy of the color; editing the copy won't directly edit the original. You can ins$$anonymous$$d edit the copy and send it back, which is what you're doing with that temp variable.
Answer by YoungDeveloper · Oct 22, 2013 at 10:35 PM
If you have problems with changing alpha on run-time, check out this topic i gave answer long time ago, should help.
http://answers.unity3d.com/questions/516744/change-texture-alpha-value-on-runtime.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613120447im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
guiTexture color half alpha White? 1 Answer
Modifying and Saving a GUITexture 0 Answers
How to Make a Character Flicker? 1 Answer