- Home /
Storing the alpha value of a canvas object to a variable
Hi all,
Puzzling on yet another challenge in this (for me) new language. What I'm trying to do is toggle the visibility of a Panel object (belonging to the Canvas). However, while other GameObjects I've encountered thusfar have a Mesh Renderer, this item has a Canvas Renderer -- which doesn't carry the rend.enabled/disabled function. Why, Unity, why.
So instead, I'm trying to use the alpha value (transparancy) to toggle the visibility. Making it appear the first time is no biggie. However, making it disappear again is problematic. After a first if-statement, in which I check whether the space bar is pressed and the Bouncing function is activated, I tried using the .GetAlpha function to test the alpha value against 1.0; this would check whether it was already visible, so then it could be turned invisible. So it'd be:
if(cv.GetAlpha=1.0f) { set alpha to 0.0}
This gives the following error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer. Which made me think that the alpha value must be stored in a variable, before I can check it against another value. However, when I tried storing it into a float, I get error CS0428: Cannot convert method group GetAlpha' to non-delegate type
float'. Consider using parentheses to invoke the method. Now I can't really figure out which type of variable I should store it as, because I used SetAlpha to give it a float value earlier in the code, and that works just fine.
Below is the code I'm trying to debug. If anyone has a solution, I'd be very grateful! public class visibility_panelBP : MonoBehaviour {
public CanvasRenderer cv;
public Bouncing startBPBouncing;
private float value_alpha;
// Use this for initialization
void Start () {
startBPBouncing = GameObject.Find("Bloedplaatjes").GetComponent<Bouncing>();
cv = GetComponent<CanvasRenderer> ();
cv.SetAlpha(0.0f);
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown ("space") && startBPBouncing.enabled == true) {
value_alpha = cv.GetAlpha;
cv.SetAlpha(1.0f);
if(cv.GetAlpha=1.0f){
cv.SetAlpha(0.0f);
}
else {
cv.SetAlpha(1.0f);
}
}
}
}
Answer by spiceboy9994 · May 27, 2015 at 05:18 PM
GetAlpha is a method... so you need to do
cv.GetAlpha()
http://docs.unity3d.com/460/Documentation/ScriptReference/CanvasRenderer.GetAlpha.html
Regards
Wow. Thank you. Excuse me while I throw myself off a building for missing that.
@Annig : Please, tag the question as resolved by validating spiceboy9994 's answer ! ;)
@Hellium: How do I do that, exactly? I was under the impression that when posting an answer as opposed to a comment, the question is consequently tagged as answered. Sorry, I'm really just starting to find my way around ^^;
@Annig : Just under the down vote button of spiceboy9994's answer (on the left), there is a check mark. Just click on it ! ;)