- Home /
How do I get the current alpha value of a material?
I've got a GUIText that I'm trying to get the current alpha value of before I start a fade. I'm having trouble with the code below always returning 0.
public float CurrentMessage1Alpha;
void Update () {
GameObject Message1 = GameObject.Find("Messages1");
CurrentMessage1Alpha = Message1.guiText.material.color.a;
}
What am I doing wrong?
Answer by jahroy · Oct 21, 2011 at 04:59 AM
I believe you get to the material by accessing the GUIText's transform's renderer...
Like this:
if ( Message1.guiText.transform.renderer != null ) {
print(Message1.guiText.transform.renderer.material.color.a);
}
OR...
After looking at the documentation some more, it looks like you might want to try the GUIText.renderer if that doesn't work...
if ( Message1.guiText.renderer != null ) {
print(Mesasge1.guiText.renderer.material.color.a);
}
I've never used a GUIText, but I'm pretty sure one of those will work.
Hmm, no luck with either of those. I get this error: $$anonymous$$issingComponentException: There is no 'Renderer' attached to the "$$anonymous$$essages1" game object, but a script is trying to access it. You probably need to add a Renderer to the game object "$$anonymous$$essages1". Or your script needs to check if the component is attached before using it.
Check my other answer...
Also make sure you add a $$anonymous$$aterial to your GUITexture in the Inspector.
Answer by jahroy · Oct 21, 2011 at 06:01 AM
Are you sure your alpha value isn't actually equal to 0?
I just created a GUIText in my game to test this...
First I added a material to the GUIText in the Inspector.
Then I added the following script to the object with the GUIText attached:
function Update () { var theTex : GUIText = transform.GetComponent(GUIText);
var theMat : Material = theTex.material;
Debug.Log("Material " + theMat.name + "has alpha " + theMat.color.a);
}
It prints the name of my material and the value 1 over and over.
This makes me wonder... Is your value actually 0?
Your answer
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
Modifying the Arial material for fading GUITexts 1 Answer
Changing two different objects renderer colour 1 Answer
Can't animate Material.Color properties 3 Answers
Lights colored like material 0 Answers