Can't link text to script

Hi, I am a unity beginner and I’ve made a score counter code, here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Score : MonoBehaviour
{
    public int score;
    public UnityEngine.UI.Text ThisIsTheScore;


    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
       
    }
    void addScore()
    {
        score ++ ;
        ThisIsTheScore.text = "Score: " + score;
    }
    private void OnTriggerEnter2D(Collider2D other)
    {
        addScore();
       
    }
}

But, when I try to link the text in the unity editor from the hierarchy to the script, it just doesn’t let me:

What am I doing wrong here? I have tried everything…

1 Like

Does that UI thing have a Text on it? Or is it actually TextMeshPRo? They’re different. Also, all errors gone from console? Any error prohibits all function.

3 Likes

Please upload another video or screenshot depicting the actual Text object you’re trying to attach to the script. If it isn’t attaching you may be using TextMeshPro on the object and not in the script, lord knows I’ve done that plenty of times!

If you do happen to be using a TextMeshPro object, you’re in luck, you can attach the object you have into your script by changing:

To:

public TextMeshProUGUI thisIsTheScore;
6 Likes

I changed it but i’m still not allowed to attach it?

Oh, ok. I’ve managed to attach it now. Thank you.

So you know for the future, there are dedicated forums for Scripting and UI you should use for such questions.

Thanks.