Having Trouble Figuring out how to get text into my drag and drop scripts in unity

50 Views Asked by At

So I am a beginner at unity and am slowly making a game but, I ran into this problem and I can't seem to figure out how to solve it. You see I have several text Mesh Pro UGUI objects within a canvas which is within a scene and I am trying to get a public script to edit the text (essentially filling the text boxes with info) The problem is that I can't seem to drag the text boxes out of the scene directly into the display and when I try making a prefab in assets the text dosen't change and when I try to give the canvas the script it no longer seems to get the global variables that I need. So how can I drag text objects from a scene into a public script so that the script can edit the assets without any issues?

Like I said before I have tried a few thing already and yes I am using Text Mesh Pro - Text (UI) with Text Mesh Pro UGUI so the typing should match. I just can't seem to take them from the scene into the public script.

Here is what my workspace looks like Here is what I am Trying to do

2

There are 2 best solutions below

3
Zbajnek On

If you want to make a reference for the TextMeshPro UI object, you need to make the reference in the script:

public TextMeshProUGUI myText;

After that you should be able to see the reference in the inspector.

0
Zbajnek On

I see what you've been doing. In order to fill the references in the script, the script itself needs to be attached on some gameobject. In your case, I would probably create an empty object called something like "Display Manager" and attach your DisplayCard.cs script on that GameObject like a component.

Like This

That's how the script is executed, by the way. The script needs to be on some GameObject, so Unity can call MonoBehavior methods on that script.