Search Unity

Question Cannot implicitly convert type 'string' to 'TMPro.TextMeshProUGUI'

Discussion in 'Scripting' started by aiyan21, Mar 13, 2023.

  1. aiyan21

    aiyan21

    Joined:
    Mar 1, 2023
    Posts:
    21
    Hello im trying to put a string to a textmeshpro text but this error keeps showing up.
    upload_2023-3-13_19-53-55.png
    here's my codes:
    Code (CSharp):
    1.  
    2.     [SerializeField]
    3.     public TextMeshProUGUI resulttxt;
    4.     [SerializeField]
    5.     public TextMeshProUGUI explanationtxt;
    6.     private string infp;
    7.  
    8.  
    9.     void Start()
    10.     {
    11.         resulttxt.text = PlayerPrefs.GetString("typeA") + PlayerPrefs.GetString("typeB") + PlayerPrefs.GetString("typeC") + PlayerPrefs.GetString("typeD");
    12.         ExplainType();
    13.     }
    14.  
    15.     void ExplainType()
    16.     {
    17.         if (resulttxt = "INFP")
    18.         {
    19.             infp = "ate angel ganda";
    20.             explanationtxt.text = infp;
    21.  
    22.         }
    23.     }
    UI view:
    upload_2023-3-13_19-54-0.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Here is how you can fix your own typing mistakes:

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    Some help to fix "Cannot implicitly convert type 'Xxxxx' into 'Yyyyy':"

    http://plbm.com/?p=263
     
  3. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    657
    Incorrect...

    Code (CSharp):
    1.         if (resulttxt = "INFP")
    2.         {
    3.             infp = "ate angel ganda";
    4.             explanationtxt.text = infp;
    5.         }
    Corrected...

    Code (CSharp):
    1.         if (resulttxt.text  == "INFP")
    2.         {
    3.             infp = "ate angel ganda";
    4.             explanationtxt.text = infp;
    5.         }
     
  4. aiyan21

    aiyan21

    Joined:
    Mar 1, 2023
    Posts:
    21
    I fixed it already I figured I can just put strings directly to the text, but thank you guys anyways!!
    Code (CSharp):
    1.  void ExplainType()
    2.     {
    3.         //ANALYSTS=================================================
    4.         if (resulttxt.text == "INTJ")
    5.         {
    6.             explanationtxt.text = "Thinkers who are creative and strategic, with a plan for everything.<br><br>" +
    7.                 "INTJs are analytical problem solvers who want to improve systems and processes with their creative ideas. <br><br>" +
    8.                 "Suitable Strands:<br> - ICT<br> - STEM";
    9.  
    10.         }