Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

I just want to change the color of a button in c# code

Discussion in 'UGUI & TextMesh Pro' started by hollywoodcolor, Aug 22, 2014.

  1. hollywoodcolor

    hollywoodcolor

    Joined:
    Jul 12, 2014
    Posts:
    35
    I just want to change problem1.jpg this value in code
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Code (csharp):
    1. GetComponent<Image>().color = Color.green
     
    tsondhi2006 likes this.
  3. hollywoodcolor

    hollywoodcolor

    Joined:
    Jul 12, 2014
    Posts:
    35
    I tried that and got :
    Assets/changeColor.cs(8,30): error CS0246: The type or namespace name `Image' could not be found. Are you missing a using directive or an assembly reference?

    ps
    can I do word wrap in the console window?
     
  4. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Code (csharp):
    1. GetComponent<UnityEngine.UI.Image>().color = Color.red;
    ps
    no
     
  5. hollywoodcolor

    hollywoodcolor

    Joined:
    Jul 12, 2014
    Posts:
    35
    Thank you that worked!!!
    I would have never guessed that.

    Has anyone made a list of inspector variables and there direct code equivalences?
     
  6. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Interesting notion, however I've never seen one such list before.
     
  7. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,670
  8. Gigiwoo

    Gigiwoo

    Joined:
    Mar 16, 2011
    Posts:
    2,981
    After getting this TOO many times, it's become my habit to add this to EVERY script.

    Code (CSharp):
    1. using UnityEngine.UI;
    Gigi
     
    Geekforlife likes this.
  9. hollywoodcolor

    hollywoodcolor

    Joined:
    Jul 12, 2014
    Posts:
    35
    that is great if your a hardcore coder, I can't find : GetComponent<UnityEngine.UI.Image>().color
    and I know what I'm looking for.

    Unity has a (Open Reference for Image) but in the manual there is no code equivalency or a link to the ScriptReference page.
     
  10. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    If you do what Gigi says, then you wouldn't need two write the fully qualified name. Just GetComponent<Image>() would work fine.