Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to convert text code to Text TMP?

Discussion in 'Editor & General Support' started by Unity-Student0, Jul 26, 2022.

  1. Unity-Student0

    Unity-Student0

    Joined:
    Aug 10, 2020
    Posts:
    263
    Hello,
    The question is not how to create or add items in Inspector to make it similar from Txt to TxtTMP but how to convert code in visual studio from Text to TextTMP. Let me be VERY clear so no one is confused and gives answers which are not relevant. I'm currently following the tutorial in https://gamedevbeginner.com/how-to-make-countdown-timer-in-unity-minutes-seconds/ and it uses Text in canvas which is outdated - as in Legacy. How do I change the code in visual studio so that it acceps TxtTmp? Be wary that this means the code you are giving is actually the updated version that the tutorial has not given yet. That means I'll be following your steps rather than what the tutorial is giving. Can anyone help?
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please share the code you need help with. I went to the link and I stopped looking after scrolling a few pages.
     
  3. Unity-Student0

    Unity-Student0

    Joined:
    Aug 10, 2020
    Posts:
    263
    Go to https://gamedevbeginner.com/how-to-make-countdown-timer-in-unity-minutes-seconds/.
    Go to the video under "How to make a timer in Unity (using Time.deltaTime)"
    Follow from 0:00 to 1:53. In the time breakdowns in the video itself, it means you would have reached the "basic timer in Unity" if you check the index. If you follow the video and create Text using Legacy, it definitely won't work.

    For the record, another unrelated youtube video shows me how to create a project so Text TMP can be detected. That's how I repaired this project. However, this means the rest of this project remains irrelevant if I use the new Text TMP. So can your unity development team create a tutorial similar to this? At current time, I'm forced to use Legacy / Text because I'm unsure how Text TMP can be used. Please do not say things like this website contains copyright information as I expect you to come up with other possibly easier examples. I don't believe this website is the only example to teach beginners. Can someone in your unity development team be nice enough to come up with a new or easier example and put this in the unity learn website? That would be nice.
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please share the code here as requested, without asking us to watch a video. I doubt you are going to get a tutorial produced just for you in a timely manner.
     
  5. danosono

    danosono

    Joined:
    Jan 22, 2019
    Posts:
    45
    I had this situation before.
    I won't be able to offer a complete answer but I may be able to point you in the right direction.
    I am no expert :)

    In your code:

    1. Add a using statement to gain access to the Text Mesh Pro code.
    using TMPro;

    2. Declare text using "TextMeshProUGUI" (for UI)
    [SerializeField] private TextMeshProUGUI myText; // this will need to refer to (be linked with) a Text Mesh Pro text item in the editor (not regular text)

    3. Henceforth, refer to your text as TMP; myText refers to a TMP object and not the older Text object
    myText.SetText(myString);

    Have a super day and hope your project goes well!

    Danny
     
    Kurt-Dekker likes this.
  6. Unity-Student0

    Unity-Student0

    Joined:
    Aug 10, 2020
    Posts:
    263
    I wish there were more people like you. But it still doesn't work. See next reply. My code is there.
     
  7. Unity-Student0

    Unity-Student0

    Joined:
    Aug 10, 2020
    Posts:
    263
    OK I'm pasting it here. This is also code modified by Danny. Can someone try? The project is gone. Nothing works.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class Timer : MonoBehaviour
    8. {
    9.     public float timeValue = 90;
    10.     public Text TimeText;
    11.     [SerializeField] private TextMeshProUGUI myText;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.  
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         if (timeValue > 0)
    23.         {
    24.             timeValue = timeValue - Time.deltaTime;
    25.         }
    26.         else
    27.         {
    28.             timeValue = 0;
    29.         }
    30.         DisplayTime(timeValue);
    31.  
    32.     }
    33.  
    34.     void DisplayTime(float timeToDisplay)
    35.     {
    36.         if (timeToDisplay > 0)
    37.         {
    38.             timeToDisplay = 0;
    39.  
    40.         }
    41.  
    42.         float minutes = Mathf.FloorToInt(timeToDisplay / 60);
    43.         float seconds = Mathf.FloorToInt(timeToDisplay % 60);
    44.  
    45.         myText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
    46.  
    47.     }
    48. }
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    The entire point of your post (starting from the subject line) is to migrate away from Text to TextMeshPRO, correct?

    If so, then get rid of line 10:

    That's the thing you are moving away from. Leaving it behind is a red herring increasing confusion, plus it gives the opportunity to make mistakes. We want to decrease confusion and mistakes since we have plenty of both of those already in the software world.

    Second, read line 36 and line 38 and explain what is going on and why you are doing that.

    Code (csharp):
    1.        if (timeToDisplay > 0)
    2.         {
    3.             timeToDisplay = 0;
    4.         }
    Do you perhaps mean to ask if
    timeToDisplay
    is LESS than zero, then set it to zero?

    Can you explain what this means?
     
    Last edited: Jul 27, 2022
  9. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    It worked with the original text object, correct? Get the basics working first. But I'm not sure about the logic, as Kurt mentioned.
     
  10. Unity-Student0

    Unity-Student0

    Joined:
    Aug 10, 2020
    Posts:
    263
    No, the original also doesn't work. Countdown only works in the inspector. It doesn't show in the design screen. So i'm unsure what to do.

    LIne 36-38 was given by the tutorial. That's why I just followed. I'm not sure if that should be removed. Do I remove?
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Nope, it was not. Go look at your link. I just did.

    Here is a snapshot of the video... it is LESS than:

    Screen Shot 2022-07-27 at 8.44.10 AM.png

    Remember there are TWO steps to doing tutorials:

    Step #1 copy it perfectly

    Step #2 go back and understand what all the parts do.
     
    Last edited: Jul 27, 2022
  12. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Why are you trying to customize if you haven't got the original working first? When doing a tutorial, it's best to follow along exactly and get it working. THEN add any customizations. And you have the timeToDisplay less than/greater than signs mixed, your code is different than the video.
     
    Kurt-Dekker likes this.
  13. Unity-Student0

    Unity-Student0

    Joined:
    Aug 10, 2020
    Posts:
    263
    I'm following the video. See my screenshot. Lines 36-38 has been removed. Now strange error messages appear. I'm unsure what is happening. I have no idea how to continue. I think screenshot is the best so everyone is clear what kind of results I am getting.


    Countdown screen2.jpg
     

    Attached Files:

  14. Unity-Student0

    Unity-Student0

    Joined:
    Aug 10, 2020
    Posts:
    263
    I did follow the tutorial. I followed the video as given at the top. It did NOT work. So I'm not sure what to do. The original tutorial just doesn't work.
     
  15. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Click on the error message. What GameObject is highlighted?
     
  16. Unity-Student0

    Unity-Student0

    Joined:
    Aug 10, 2020
    Posts:
    263
    It doesn't give any code or filename.
     

    Attached Files:

  17. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Don't remove the lines, just make them the same as in the video. You had your < and > signs mixed.
     
  18. Unity-Student0

    Unity-Student0

    Joined:
    Aug 10, 2020
    Posts:
    263
    Where did I get them wrong?
     
  19. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Line 36 as has been pointed out several times.
     
  20. Unity-Student0

    Unity-Student0

    Joined:
    Aug 10, 2020
    Posts:
    263
    I have corrected it to
    Code (CSharp):
    1.         if (timeToDisplay < 0)
    2.  
    The results are the same. I still get strange error messages. Can you or others advice what next?
     
  21. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    I'm not sure how many more times it can be pointed out, so I will simply direct you to read the entirety of my post #11 above, which now includes a screenshot of the video showing LESS THAN as being correct:

    https://forum.unity.com/threads/how-to-convert-text-code-to-text-tmp.1314291/#post-8315997

    The two most obvious choices I see are:

    1. work through the tutorial and check every step of your work, OR

    2. throw out everything you have and restart the tutorial from scratch.

    Either way, I absolutely positively stand by my process for doing tutorials correctly, which I will include here for your benefit:

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!

    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
     
  22. Unity-Student0

    Unity-Student0

    Joined:
    Aug 10, 2020
    Posts:
    263
    I have actually completed the tutorial and the code given is now considered finished. So review them again?
     
  23. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Up to you. For something as simple as 48 lines of code, half of which are blank / formatting, I'd throw it out and start afresh, but that's just me. I like crystal clarity when I'm learning something new.
     
  24. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    If you are still struggling, please make a back up first, then delete the /Library folder, zip it up, and attach. I'll take a look.
     
  25. Unity-Student0

    Unity-Student0

    Joined:
    Aug 10, 2020
    Posts:
    263
    Hi @JeffDUnity3D,
    I've re-created a new project and used Legacy instead of Text TMP. I completed it in a few minutes and the results are great. I don't get any kind of strange error message in the status. Now it's back to my original question : Can your team create a useful, comprehensive tutorial by using Text TMP? I hope it won't take at least 1 month or 2 and requires approval from some management at some high level. I still don't understand why an internet search usually does better than your unity learn team. I hope you can give a reply.
     
  26. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    I'm glad you were able to make progress. Have you converted to TMP Text yet, per the suggestions in this thread? That's your next step! This might help https://docs.unity3d.com/Packages/com.unity.textmeshpro@3.0/manual/index.html
     
    Kurt-Dekker likes this.
  27. Unity-Student0

    Unity-Student0

    Joined:
    Aug 10, 2020
    Posts:
    263
    Yes. Finally solved. I also compared what I had with what Danny gave earlier. The solution is to give the variable name the same as the Text only. Then it works well.

    However, you need to see the next thread I'm going to open.
     
  28. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    No thank you, you seem to be making the same silly typos. I would suggest that you slow down while going through the tutorials. Or find a tutorial series that provides the complete source code to compare to. Or join the Patreon groups for those persons making these tutorials and perhaps offer a donation so they can make a living too. Then you can talk to the tutorial developers directly. And make sure to first go through ALL the comments at the bottom of these tutorials to see if others have run into the same issues.
     
    Kurt-Dekker likes this.