Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Can't link text to script

Discussion in 'Getting Started' started by SootyHamster, Mar 4, 2022.

Thread Status:
Not open for further replies.
  1. SootyHamster

    SootyHamster

    Joined:
    Mar 6, 2021
    Posts:
    8
    Hi, I am a unity beginner and I've made a score counter code, here is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Score : MonoBehaviour
    7. {
    8.     public int score;
    9.     public UnityEngine.UI.Text ThisIsTheScore;
    10.  
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.        
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.        
    22.     }
    23.     void addScore()
    24.     {
    25.         score ++ ;
    26.         ThisIsTheScore.text = "Score: " + score;
    27.     }
    28.     private void OnTriggerEnter2D(Collider2D other)
    29.     {
    30.         addScore();
    31.        
    32.     }
    33. }
    34.  
    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...
     
    Thictony likes this.
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,067
    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.
     
    Renchaa, bpgomesDev and Thictony like this.
  3. Thictony

    Thictony

    Joined:
    Jul 24, 2021
    Posts:
    3
    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:
    Code (CSharp):
    1. public TextMeshProUGUI thisIsTheScore;
     
  4. SootyHamster

    SootyHamster

    Joined:
    Mar 6, 2021
    Posts:
    8
    I changed it but i'm still not allowed to attach it?
     
  5. SootyHamster

    SootyHamster

    Joined:
    Mar 6, 2021
    Posts:
    8
    Oh, ok. I've managed to attach it now. Thank you.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,589
    So you know for the future, there are dedicated forums for Scripting and UI you should use for such questions.

    Thanks.
     
Thread Status:
Not open for further replies.