Search Unity

help me understand why I can't set my TextMeshPro color this way?

Discussion in 'Scripting' started by stain2319, Apr 10, 2021.

  1. stain2319

    stain2319

    Joined:
    Mar 2, 2020
    Posts:
    417
    I am looking to set the color of a TextMeshPro text object based on a variable.

    This works:

    Code (CSharp):
    1. textMeshPro.color = Color.cyan;
    This does not:

    Code (CSharp):
    1. Color myColor = Color.cyan;
    2. textMeshPro.color = myColor;
    3.  

    Error CS0120 An object reference is required for the non-static field, method, or property 'FloatingMessage.defaultColor'

    Essentially I want to be able to do something like:

    Code (CSharp):
    1. public void ChangeMessage(string message, Color whichColor)
    2. { textMeshPro.SetText(message);
    3.   textMeshPro.color = whichColor;
    4. }
    5.  
    But I cannot for the life of me figure out how to make it work...
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Check closely again... I don't think the code lines you snipped are related to the error... the reason is there's nothing to do with defaultColor in those code lines.

    For colors in general, TMPro is weird: you can set a color in the font asset itself, and that gets multiplied by whatever is in the presentation object, so generally keep your fonts pure white.
     
  3. stain2319

    stain2319

    Joined:
    Mar 2, 2020
    Posts:
    417
    I changed the variable name in between snips, it's correct though. Just pretend myColor says defaultColor. but in any case I will check into what you said.