Search Unity

GUI.color.a in C#

Discussion in 'Immediate Mode GUI (IMGUI)' started by madwilson, Jul 17, 2008.

Thread Status:
Not open for further replies.
  1. madwilson

    madwilson

    Joined:
    Jun 4, 2008
    Posts:
    106
    is there a way to set a gui alpha like you can in java
    this works in Java will not work in C#
    Code (csharp):
    1. GUI.color.a = 0.5;
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Try 0.5f
     
  3. madwilson

    madwilson

    Joined:
    Jun 4, 2008
    Posts:
    106
    that didn't work..

    Assets/Standard Assets/GUI/GUIscript.cs(187) error: Cannot modify the return value of `UnityEngine.GUI.color' because it is not a variable
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (csharp):
    1. Color thisColor = GUI.color;
    2. thisColor.a = 0.5f;
    3. GUI.color = thisColor;
    --Eric
     
    rmele09 likes this.
  5. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Here is where you learn that the documentation has been lying to you, and that some variables are actually not variables but things called "properties".

    Properties are accessed like variables, but they actually call functions which get or set values. In the case of GUI.color (and any property that returns a struct), you don't get access to the original colour of the GUI, but rather you get a copy of it. Changing the alpha component of the copy does not affect the actual GUI.color. Instead, you have to separate copying the colour out, modifying it, and copying it back in.

    This may seem convoluted, but Javascript actually does the same thing. UT built a shortcut into the compiler so that beginners weren't exposed to the complexity of the process, but there is no such shortcut provided in C#.

    You might ask (quite rightly), "why am I using C# if it just requires more lines of code to do the same thing?"

    The answer has two parts. First, C# doesn't always take more lines, and there are cases where it takes less. Second, by not using shortcuts, C# gives you a better understanding of what you're doing. C# has other advantages which aren't related to UT-provided shortcuts, but I won't go into them here.
     
  6. madwilson

    madwilson

    Joined:
    Jun 4, 2008
    Posts:
    106
    I have got to say that when I get stuck and ask a question that I can't fined out in the documentation or on a search the community steeps up not only to help get the question answered but to give a reason why thanks!
     
  7. p87

    p87

    Joined:
    Jun 6, 2013
    Posts:
    318
    Since this is still being indexed by google,

    Code (csharp):
    1.  
    2. GUI.color = new Color() {  
    3.      a = 0.5f
    4. };
    5.  
    6. // shorthand
    7. GUI.color = new Color() { a = 0.5f };
    8.  
     
Thread Status:
Not open for further replies.