Search Unity

Image Color doesnt change !!!!

Discussion in 'Scripting' started by TTaspar, Sep 17, 2017.

  1. TTaspar

    TTaspar

    Joined:
    Feb 10, 2016
    Posts:
    6
    Hey , i wanna change the image.color but i have a problem.


    when i hit the play,



    the color stay yellow, doesnt go back red ??? G increases but color doesnt change ??? by the way, sorry about my english :),


    This is my code;
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ChangingColorRGB : MonoBehaviour {
    7.  
    8.     float G = 75;
    9.  
    10.     Image imgcolor;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         imgcolor = this.GetComponent<Image>();
    15.  
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void FixedUpdate () {
    20.         Changing();
    21.     }
    22.  
    23.     void Changing()
    24.     {
    25.         if(G < 255)
    26.         {
    27.             if(G == 254)
    28.             {
    29.                 G = 75;
    30.             }
    31.             G += 1;
    32.          
    33.             imgcolor.color = new Color (255, G, 0, 255);
    34.             print(G);
    35.          
    36.         }
    37.  
    38.      
    39.     }
    40. }
    41.  
     
    Last edited: Sep 17, 2017
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    I've hit this "issue" myself, too :)
    There are '2' kinds of Color variables in Unity. Color and Color32. Color is in the range 0..1 and is a float. the syntax you're using is for Color32. So, you can choose to alter it how you'd like so it works ;)
     
    Xepherys likes this.
  3. Xepherys

    Xepherys

    Joined:
    Sep 9, 2012
    Posts:
    178
    Just as an aside, the benefit of Color in the range (0..1) is that it can represent a position on a gradient. I use this to flicker softly between yellows and oranges for flames. Color32 uses (0-255) values for RGBA.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,870
    And to add, you can simply divide your value by 255 to get the float you want.

    So if G = 75, just do G/255.
     
    Xepherys likes this.
unityunity