Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Problems with changing the colour of text through script

Discussion in 'Scripting' started by Hotpots, Oct 23, 2015.

  1. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Hey guys,
    So what I want to do is once a user collects an item a number will appear in the middle of the screen then fade away.

    Green text - When an item is picked up.
    Red text - When an item is dropped.

    What I have so far:

    Code (CSharp):
    1.     public Text bookweightA;
    2.     public Text bookweightS;
    3.  
    4.     public Text mapweightA;
    5.     public Text mapweightS;
    6.  
    7.     Color visibleG = new Color(124f, 255f, 130f, 100f);
    8.     Color visibleR = new Color(255f, 124f, 124f, 100f);
    9.  
    10. void OnTriggerStay(Collider other)
    11.     {
    12. ...
    13. bookweightA.color = visibleG;

    Now the problem I'm having is that the text is not changing colour, it just appears white, however the alpha values works! :)
     
  2. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    I believe color values are from 0.0 to 1.0 :p
     
  3. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    I tried something different instead and got the desired color, how would I fade it back out?
    This is my attempt thus far?

    Code (CSharp):
    1.             Color c = bookweightA.color;
    2.             c.a = 100;
    3.             bookweightA.color = c;
    4.             timeleft -= Time.deltaTime;
    5.             if (timeleft == 0)
    6.             {
    7.                 c.a = 0;
    8.                 bookweightA.color = c;
    9.             }
     
  4. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    This is the output of my debug.log of the timeleft variable everytime I pick up and drop the object.

    Anybody know why?

    I declared timeleft at the top of the script:
    Code (CSharp):
    1. float timeleft = 2.0f;
     
  5. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    So I picked up and dropped my object up roughly 200 times and as the timer become equal to or less than 0 my text disappeared as desired, so there is a problem with my timer pausing? Anyone know how I can fix it? :(


    EDIT: Is utilising a timer the best way about going this, is there a way I can do something like...
    Wait 2 seconds
    Change alpha of the text back to 0.?
     
    Last edited: Oct 23, 2015
  6. DuffyT

    DuffyT

    Joined:
    Dec 16, 2014
    Posts:
    37
    I suggest you take a look at coroutines. :')
     
  7. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    How would I go about doing?
    Is it best to utilise co-routines for this problem or just a simple timer?
     
  8. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
  9. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Thanks Fajl, this has helped me get a step further towards my solution.
    I used CrossFadeAlpha() instead, it works perfectly as I wanted the first time I pick up the item.
    The text component appears "+1.2kg" then fades after 2secs, however if I drop the item and try pick it up again it does not work :(

    Code (CSharp):
    1.         if (other.gameObject.tag == "book" && Input.GetKey("e"))
    2.         {
    3.             itemweight = 1.2f;
    4.             ScoreManager.currentweight += itemweight;
    5.             other.gameObject.SetActive(false);
    6.             bookimage.color = visible;
    7.             isholding = true;
    8.             Color c = bookweightA.color;
    9.             c.a = 100;
    10.             bookweightA.color = c;
    11.             bookweightA.CrossFadeAlpha(0, 2, false);
    Full script (if needed):
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class ObjectPickup : MonoBehaviour
    6. {
    7.     public Image bookimage;
    8.     public Image mapimage;
    9.  
    10.     public Text bookweightA;
    11.     public Text bookweightS;
    12.  
    13.     public Text mapweightA;
    14.     public Text mapweightS;
    15.  
    16.     public static float itemweight;
    17.  
    18.     bool isholding;
    19.     //float timeleft = 2;
    20.     Color visible = new Color(0f, 0f, 0f, 100f);
    21.     Color invisible = new Color(0f, 0f, 0f, 0f);
    22.  
    23.     GameObject book;
    24.     GameObject map;
    25.  
    26.     void Start()
    27.     {
    28.         book = GameObject.FindGameObjectWithTag("book");
    29.         map = GameObject.FindGameObjectWithTag("map");
    30.         //anim = GetComponent<Animator>();
    31.     }
    32.  
    33.  
    34.     void Update()
    35.     {
    36.         if (Input.GetKey("q") && isholding == true)
    37.         {
    38.             DropItem();
    39.         }
    40.     }
    41.  
    42.  
    43.     void OnTriggerStay(Collider other)
    44.     {
    45.         // If the entering collider is a book...
    46.         if (other.gameObject.tag == "book" && Input.GetKey("e"))
    47.         {
    48.             itemweight = 1.2f;
    49.             ScoreManager.currentweight += itemweight;
    50.             other.gameObject.SetActive(false);
    51.             bookimage.color = visible;
    52.             isholding = true;
    53.             Color c = bookweightA.color;
    54.             c.a = 100;
    55.             bookweightA.color = c;
    56.             bookweightA.CrossFadeAlpha(0, 2, false);
    57.             /*timeleft -= Time.deltaTime;
    58.             Debug.Log(timeleft);
    59.             if (timeleft <= 0)
    60.             {
    61.                 c.a = 0;
    62.                 bookweightA.color = c;
    63.                 timeleft = 0;
    64.             }*/
    65.         }
    66.         // If the entering collider is a map...
    67.         else if (other.gameObject.tag == "map" && Input.GetKey("e"))
    68.         {
    69.             itemweight = .2f;
    70.             ScoreManager.currentweight += itemweight;
    71.             other.gameObject.SetActive(false);
    72.             mapimage.color = visible;
    73.             isholding = true;
    74.             Color c = mapweightA.color;
    75.             c.a = 100;
    76.             mapweightA.color = c;
    77.         }
    78.         else
    79.         {
    80.         }
    81.     }
    82.  
    83.        
    84.     void DropItem()
    85.     {
    86.         if (bookimage.color == visible)
    87.         {
    88.             itemweight = 1.2f;
    89.             ScoreManager.currentweight -= itemweight;
    90.             bookimage.color = invisible;
    91.             Vector3 position = GameObject.Find("CollectableSpawnPoint").transform.position;
    92.             GameObject item = Instantiate(book, position, Quaternion.identity) as GameObject;
    93.             item.gameObject.tag = "book";
    94.             item.gameObject.SetActive(true);
    95.         }
    96.         if (mapimage.color == visible)
    97.         {
    98.             itemweight = .2f;
    99.             ScoreManager.currentweight -= itemweight;
    100.             mapimage.color = invisible;
    101.             Vector3 position = GameObject.Find("CollectableSpawnPoint").transform.position;
    102.             GameObject item = Instantiate(map, position, Quaternion.identity) as GameObject;
    103.             item.gameObject.tag = "map";
    104.             item.gameObject.SetActive(true);
    105.         }
    106.         isholding = false;
    107.     }
    108. }
     
    Last edited: Oct 24, 2015
  10. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    I'm still unsure why CrossFadeAlpha() isn't working after dropping the item once :(