How I Solve this error, 21

Preview is correct but shows an error,


This is an error on line 16. You have a variable called textComponent. You are trying to access a property on textComponent called “.text” but textComponent is emtpy (null), and therefore has no data at all. No properties at all. So you can’t access “.text” property if there are no properties.

Normally there would be a .text property because textComponent is of type Text, and all Text objects have a “.text” component built in. The problem is this variable is capable of holding a Text component but it doesn’t have one yet.

You haven’t told it which Text component to refer to.

You probably only have one and so it might seem obvious to you, but what if you had hundreds? You need to tell textComponent WHICH text object it’s supposed to reference so it knows which text to change.

Normally this is done witht he assignment operator “=” but in this case, you’ve used a [SerializeField] attribute, which allows you go into Unity to assign the reference.

You need to go into Unity, highlight that object with this script, and then drag an object with a Text component into the Text field. The dragged object must come from your hierarchy, not below in the project assets.

I’m assuming this script is on your Game object, but I’m not sure. Just click on a text from the hierarchy and drag it in to the Text field. That will assign that text to that field during initialization, even if you did not specify one by code. (Even if you did, this will override it.)

I already assigned a reference of the text component

Hi @Fenil_Lakhani,

In the second screenshot of your first post, I can see an asterisk next to the filename in the tab. This indicates that the script was not saved. Make sure the script is saved.

image

If the issue persists, remove the reference to the Text Component in your Inspector, then reassign it.

1 Like

Thank you :grinning_face_with_smiling_eyes:, it’s done now

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

Privacy & Terms