Search Unity

Question TextMeshPro, Text Inputs

Discussion in 'Editor & General Support' started by ronbolaw, Dec 2, 2021.

  1. ronbolaw

    ronbolaw

    Joined:
    Jan 26, 2019
    Posts:
    2
    I have one canvas attached to a child object. I have two texts which are words. I have one Text that is actually a number for tracking a velocity and one slider tracking fuel level.

    My problem is whenever I use "AtvVelo1 = FindObjectOfType<TextMeshProUGUI>();" It always grabs one of the text inputs that are words.

    The question is how do you tell unity which of the text inputs you want to access? Even if I put the other one first, it still grabs the wrong one and if I turn off the one I don't want it grabs none of them

    Do I need another canvas or is there a way to use a find with tag on TextMeshProUGUI?




    AtvVelo1 = FindObjectOfType<TextMeshProUGUI>();
    string temp1 = (Velocity).ToString("0");
    AtvVelo1.SetText(temp1);
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Always the same answer for Unity, ALWAYS... it's the most common workflow and every tutorial or example project uses it: you make and assign the fields:

    Code (csharp):
    1. public TextMeshProUGUI VelocityReadout;
    2. public TextMeshProUGUI FuelReadout;
    Love it, embrace it, your Unity experience will be FAR smoother than trying to Find stuff on the fly.

    Save the tricky-tricky "Find on the fly" stuff for when you have a lot of time in Unity and understand why what you did above isn't working.
     
  3. ronbolaw

    ronbolaw

    Joined:
    Jan 26, 2019
    Posts:
    2
    Thank you so much, that worked instantly. It is what I had but I was recalling the TextMeshProGUI again without the reference so it always reverted to the other text.