Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Change 3DText font color?

Discussion in 'Scripting' started by carking1996, Sep 24, 2011.

  1. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,563
    Hello, I know how to do this, but how do I make it only change the one font color that I want to change?
     
  2. RaskVann

    RaskVann

    Joined:
    Jul 17, 2011
    Posts:
    82
    Save the textMesh you want to change and use the following.

    You're not even trying: Search it next time
     
  3. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,563
    Hey, I saw that... I said I knew how to change the color. But it changes all of them in every scene to that color.
     
  4. RaskVann

    RaskVann

    Joined:
    Jul 17, 2011
    Posts:
    82
    Read all of my input. Save the textMesh you want to change inside your code, then use that saved variable to change it. If you assign the same function to everything and you say .font.material = Color.red or something similar they are all going to run the same thing. Your not giving us any code to go by so this is harder to solve then it would otherwise be. Complete explanation would be nice (help us help you please)
     
  5. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,563
    Here ya go. :)

    Code (csharp):
    1.  
    2. var t : TextMesh;
    3.  
    4. function Start(){
    5. t = transform.GetComponent(TextMesh);
    6. }
    7.  
    8.  
    9. function OnMouseDown () {
    10.     PlayerPrefs.SetString("Car Name",  "Car5");
    11.     t.font.material.color = Color.yellow;
    12.        
    13.     }
     
  6. RaskVann

    RaskVann

    Joined:
    Jul 17, 2011
    Posts:
    82
    Are you using the above code (is it attached) to each object your having problems with?

    I'm guessing that's the case in which case that is your problem, you need a way to differentiate the ones you want to change vs the ones you don't. How does your code know which to change? Because it looks to me when your mouse is down it changes all textMesh of every object that has that script attached and changes it to yellow.
     
    Last edited: Sep 24, 2011
  7. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,563
    Yes. It is attached to the one I want to change the color of. How would I change it? I don't think you can change the name of a component.
     
  8. RaskVann

    RaskVann

    Joined:
    Jul 17, 2011
    Posts:
    82
    You don't want to change the component name, you want to find a way of figuring out what your mousing over and change only that. I personally assign everything a unique name and then use a raycast to find the name of what my mouse is on. From there I compare what I'm selecting with who's name I'm currently on or a list of things and then only change it when the names of the raycast and the name I'm looking at are the same.

    I personally use the following for my own game (note: it's in OnGUI() to make use of the mouse position).
    Code (csharp):
    1.  
    2.     if(Input.GetMouseButtonDown(0))
    3.     {
    4.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    5.         RaycastHit hit;
    6.         if(Physics.Raycast(ray.origin, ray.direction, out hit, 1000))
    7.         {
    8.             if(hit.transform.gameObject.name == WhicheverObjectYouWantToChange)
    9.             {
    10.  
    11.             }
    12.         }
    13.     }
    14.  
     
  9. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,563
    Thanks. I'll test it and tell you the results here. :)
     
  10. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,563
    I get a lot of errors.

    Code (csharp):
    1.  
    2. function OnMouseDown () {
    3.     PlayerPrefs.SetString("Car Name",  "Car5");
    4. }
    5.  
    6.  function OnGUI(){
    7.  
    8.  if(Input.GetMouseButtonDown(0)){
    9.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    10.         RaycastHit hit;
    11.         if(Physics.Raycast(ray.origin, ray.direction, out hit, 1000)){
    12.             if(hit.transform.gameObject.name == "ProbeSelectGreen"){
    13.              guiText.material.color = Color.green;
    14.             }
    15.         }
    16.     }
    17. }
    $errors.jpg

    EDIT: is your code in C#? I think it is. lemme try converting it.
     
  11. eTag96

    eTag96

    Joined:
    Oct 24, 2010
    Posts:
    110
    yeah the code is in C# and the
    Code (csharp):
    1. Ray ray = ...;
    2.  
    is supposed to be
    Code (csharp):
    1. ray : Ray = ...;
    but not just that, all variable declarations have to be edited.
     
  12. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,563
unityunity