Not using TextMeshPro - Help

25_br_cud
2d
#1

I’m not using TextMeshPro and I’m having trouble getting the Score Text box in my Canvas to recognize the Serialized Field for scoreText. There was no discussion that I could find on getting your score to display if you were just using the built-in font. I’m not sure what I’m missing. I’ve tried making the Serialized Field a string, but I cannot connect the Score Text. Any ideas?

#2

Hi Ani,

Without knowing anything about your project, it’s difficult to help you. Of what type is the field you exposed in your Inspector? If you want to assign a Text component to it, the type must be Text, not TextMeshProUGUI or something else.

#3

Sorry, this was regarding the Block Breaker project in the Udemy 2D and C# course. Specifically the lecture about displaying the player score. I thought that’s what forum I had entered. If this post is out of place, please advise and I can move it.

The SerializeField is of type String. There was no type Text avaible when declaring the variable inside of the Game Status class.

#4

You cannot assign the Text component to the string variable. Maybe it’s a good idea to rewatch the video and compare it to your project. It’s easy to miss a detail.

#5

I’ve rewatched the video. The example is based on TextMeshPro - which I’m trying not to use. He mentions you don’t have to, but doesn’t offer guidance on how to change/update the Text component’s actual text via the Game Status class.

#6

Rick uses a Text Mesh Pro UGUI component in his Inspector, and the variable in his code is of type TextMeshProUGUI.

You want to use a Text component in your Inspector, so the variable in your code must be of type ______.

String is not a component in your Inspector.

#7

Correct, but when I am in the class where I am creating the variable, Text is not a variable type that is being recognized by Visual Studio as valid. Am I not using a namespace that I should be?

#8

Am I not using a namespace that I should be?

That could be the case. Take a look at the API. There you can find the relevant information.

image

Did you include this namespace?


See also:

#9

I was missing that namespace, yes. But now that I’ve included it, I’m getting a different error:

[SerializeField] Text scoreText;
[SerializeField] int currentScore = 0;

private void Start()
{
    scoreText = currentScore.ToString();
}

I’m getting the error that currentScore.ToString(); cannot implicitly convert type ‘string’ to ‘UnityEngine.UI.Text’. I’m not sure how else I am supposed to get the score into that Text field if I cannot convert a string into Text.

#10

scoreText references the object. You want to access its text property, which is of type string. Try the following:

scoreText.text = currentScore.ToString();

#11

Nina, thank you for your help. My issue is now resolved. I’m still not familiar enough with everything yet to see that I was trying to convert a string to the text component, as opposed to the component’s actual ‘text’. Thank you for your assistance. It’s been remarkably helpful.

#12

I’m glad it worked. :slight_smile:

Actually, the principle is fairly simple. First of all, don’t get confused by names.

In Unity, you have a component. A component is a script attached to a game object. In the Inspector of a game object, you see components. Transform is a component, too. When you attach a Text or a TextMeshProUGUI, a Health script, AdventureGame or whatever, these scripts become components.

In C#, we don’t have components. We use classes as containers of data. Transform, Text, TextMeshProUGUI, Health, AdventureGame and so on are classes. They contain data, e.g. properties, methods, etc.

A class does not do anything, so you have to access things inside the class to make something happen.

In your case, you want to do something with text. In C#, a text is represented by a string. The Text class is not a string. For this reason, you have to open the API and check the properties. Is there something you could use for your idea? Yes, there is.

image

Then you click on the link to learn more about this property.

image

As you can see, it’s of type string. You want to do something with a text, you need a string, text is a string, so this is probably what you need. Since you create instances of this Text class, you access each property via the object. This means: you create a variable of type Text, assign a Text object to it and access the property via the variable name.

[SerializeField] Text scoreText;
[SerializeField] Text anotherText;
[SerializeField] Text blah;

private void Start()
{
    scoreText.text = "Hello";
    anotherText.text = "Hello";
    blah.text = "Hello";
}

When you assign three different Text objects, you can access the text property of three different Text objects. When you assign the same Text object to all variables, you would access the text property of the same Text object.

I’m explaining you this because this principle works for all components/C# scripts. For example, in the next projects, you will implement an AudioSource component. It does not have a text property but other things you might want to access.

If you want to learn more about C#, I can recommend this free C# course by Bob Tabor. According to Microsoft, it’ll get retired on 10th June 2019.

#13

Thanks for the info! I’ll check it out.

closed #14

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.