Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

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,660
  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.