Search Unity

Problem getting colour to change on a text mesh using code

Discussion in 'Scripting' started by Sunderer, Jun 30, 2015.

  1. Sunderer

    Sunderer

    Joined:
    Mar 16, 2015
    Posts:
    35
    The following code works fine when I am changing colour using <Renderer>().material.color but fails to work when i change it to <TextMesh>. I am trying to get text to change colour on a sprite. These are not UI elements but just standard sprites with a TextMesh attached.

    Code (CSharp):
    1. {
    2.     public Color col;
    3.     private Color startColor;
    4.     private Color currentColor;
    5.     private float blendValue;
    6.     private int mouseover;
    7.  
    8.     void Start()
    9.     {
    10.         startColor = transform.GetComponent<TextMesh>().color;
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         if (blendValue > 0 && mouseover == 0)
    16.         {
    17.             blendValue -= Time.deltaTime * 0.5f;
    18.             transform.GetComponent<TextMesh> ().color = Color.Lerp (startColor, currentColor, blendValue);
    19.         }
    20.     }
    21.  
    22.     void OnMouseOver()
    23.     {
    24.         if (mouseover == 0) {
    25.             currentColor = transform.GetComponent<TextMesh> ().color;
    26.             blendValue = 0;
    27.         }
    28.         mouseover = 1;
    29.  
    30.         if (blendValue < 1)
    31.         {
    32.             blendValue += Time.deltaTime * 0.5f;
    33.             currentColor = transform.GetComponent<TextMesh> ().color = Color.Lerp (currentColor, col, blendValue);
    34.         }
    35.     }
    36.  
    37.     void OnMouseExit()
    38.         {
    39.             if (mouseover == 1)
    40.             {
    41.                 currentColor = transform.GetComponent<TextMesh>().color;
    42.             }
    43.  
    44.             mouseover = 0;
    45.             blendValue = 1;
    46.         }
    47. }
     
  2. Sunderer

    Sunderer

    Joined:
    Mar 16, 2015
    Posts:
    35
    It has something to do with the text mesh object being a child of the GUI parent. When i seperate the Text as a stand alone object the text works fine. But making it a child again the colour change stops working. But why? There is nothing odd about my GUI parent:
     

    Attached Files:

  3. Sunderer

    Sunderer

    Joined:
    Mar 16, 2015
    Posts:
    35
    Ok! I have debugged further, the problem lies in the RIGIDBODY. When i remove the rigidbody the lerp works. Whu would a rigid body be affecting this code??