3D Color change every 3D Text Color

Why is that with this code, that every 3D Text in the scene change text color

using UnityEngine;
using System.Collections;

public class previewControls : MonoBehaviour {
    public static bool preview = false;
    private TextMesh t;
    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
        t = transform.GetComponent<TextMesh> ();
        t.GetComponent<TextMesh>().text = "PREVIEW";
        if (preview) {
            t.GetComponent<TextMesh> ().font.material.color = Color.green;
        } else {
            t.GetComponent<TextMesh> ().font.material.color = Color.white;
        }
    }

    void OnMouseDown ()

    {

        preview = !preview;
    }
}

Your TextMeshes all use the same shared material. When you change the material’s color, it applies to all TextMeshes that use it.

Consider using a Unity UI Text element instead. You could set a different material for each TextMesh, but you’ll probably prefer working with Text elements in the long run.

Hey TonyLI

I have just experience problem with using GUI. Like when it is in the middle of my screen on the computer and i test it on my phone, then the GUI wont stay at its original position

It sounds like you just need to set the anchors. See Basic Layout.

your preview variable is also static, so when it is set, all instances of previewControls in the scene will have their preview variable set.

TonyLi I will have a look

Killingbeck there is only one gameObject with the script “PreviewControls”