Search Unity

Changing Text Mesh color

Discussion in 'Immediate Mode GUI (IMGUI)' started by ulascan, Aug 17, 2014.

  1. ulascan

    ulascan

    Joined:
    Jun 23, 2013
    Posts:
    2
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Button : MonoBehaviour {
    6.  
    7.     public TextMesh textmesh;
    8.     public Material normally;
    9.     public Material onMouseHover;
    10.     public AudioClip hover_sound;
    11.     public bool quit = false;
    12.  
    13.  
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.  
    18.    
    19.     }
    20.    
    21.     // Update is called once per frame
    22.     void Update () {
    23.  
    24.         RaycastHit HitInfo;
    25.         bool played_sound = false;
    26.  
    27.         if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition) , out HitInfo))
    28.            {
    29.  
    30.             textmesh.color = Color.gray;
    31.             if(!played_sound)
    32.             {
    33.  
    34.                 played_sound = true;
    35.                 audio.Play();
    36.  
    37.  
    38.             }
    39.  
    40.        
    41.  
    42.            }else{
    43.  
    44.             textmesh.color = Color.white;
    45.             played_sound = false;
    46.         }
    47.    
    48.     }
    49.     void onMouseEnter() {
    50.  
    51.    
    52.    
    53.         Debug.Log("Works!");
    54.  
    55.  
    56.     }
    57.     void onMouseDown()
    58.     {
    59.  
    60.     if(quit)
    61.  
    62.         {
    63.  
    64.  
    65.  
    66.             Application.Quit ();
    67.         }
    68.  
    69.  
    70.  
    71.     }
    72. }
    73.  
    74.  
    75.  
    76.  
    77.  
    Hi
    I am fairly new to unity. Soo i am trying to create a 3d menu and i have this script that changes the color whenever mouse hovers over one of the buttons. The problem is when one of the buttons gets triggered all of them change color.
    Thanks for the replies:)
     
  2. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    To use mouse event functions(OnMouseEnter, ...) need a Collider.

    To change TextMesh color, change color TextMesh attribute.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(BoxCollider))]
    5. [RequireComponent(typeof(TextMesh))]
    6. public class ButtonTextMesh : MonoBehaviour {  
    7.  
    8.     public Color normallyColor = Color.white;
    9.     public Color mouseHoverColor = Color.red;
    10.     public bool quit = true;
    11.  
    12.     TextMesh textmesh;
    13.    
    14.     void Start(){
    15.  
    16.         textmesh = GetComponent<TextMesh>();
    17.         textmesh.color = normallyColor;
    18.    
    19.     }
    20.  
    21.     void OnMouseEnter(){ textmesh.color = mouseHoverColor; }
    22.     void OnMouseExit() { textmesh.color = normallyColor; }
    23.     void OnMouseDown() { if(quit) Debug.Log("Quit"); }
    24.    
    25. }
    Download link.
     
  3. ulascan

    ulascan

    Joined:
    Jun 23, 2013
    Posts:
    2
    Already figured it out thanks though:))
     
  4. AeroshopGraphic

    AeroshopGraphic

    Joined:
    Feb 19, 2018
    Posts:
    1
    Can you please post your solution? :)