How would i attach a text to a script?

Here is the script im not sure how to do this as every time i drag and drop the scrip to the location on unity it dosn’t work?

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

public class Itemcollector : MonoBehaviour
{
    private int cherries = 0;

    [SerializeField] private TMP_Text CherriesText;


    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Cherry"))
        {
            Destroy(collision.gameObject);
            cherries++;
            cherriesText.TMP = ("Cherries:" + cherries);
        }
    }
}

If you need an image of the text inspector then i’ll give

4 Answers

4

Does this even compile?
CherriesText became cherriesText;
TMP_Text doesn’t have a property called TMP, at least afaik;
("Cherries:" + cherries); You can’t add string and integer.

I believe that entire row should be CherriesText.text = $"Cherries: {cherries}";
But since you got to drag and dropping something, I’d recommend you to start by reading errors in the console.

Thank you, er ill see

The issue is that i cant drag and drop it just dosn’t find the asset ive looked on youtube and old feed but nothing how should i report this as a bug

Drag and drop what? Where? If your script didn't even compile, there's nothing to drag.

The script compiles but since there is no text under the script, the script dosn’t work.

Thank so much

You are posting new answers instead of comments. Anyway, I really don't know what you mean. It couldn't have compiled before.

oh ok, just found out how to reply but its compiling for sure im mean its working but i cant add the text to the script