- Home /
How do I get the Text Mesh Pro UGUI working in a prefab?
I'm working on a 3D game where you can earn money by selling items. I made a script that shows your current money. The script should be on thousands of items, but it doesn't work correctly if I drag the UI element into the (prefab) inspector. It works if I give the script to all items in the scene individually, but it has to be in the prefab. I can add the Text Mesh Pro UGUI if it is also a prefab itself, but then the system will not work properly. So I want to add the Text Mesh Pro element without being a prefab itself, how can I do it best?
Where to place the element:


The script I want to assign to the prefab (I don't know if you can do anything with this):
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using TMPro;
 
 public class Valuable : MonoBehaviour
 
 {
     public TextMeshProUGUI countText;
     public static int count;
     private Rigidbody rb;
 
     // Count = Money
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         count = 0;
     }
 
     void SetCountText()
     {
         countText.text = "Money: $" + count.ToString();
         Debug.Log("+500");
     }
 
 
 // When something goes trough the trigger, it will add money and destroy the object
 private void OnTriggerEnter(Collider other)
     {
         if(other.gameObject.CompareTag("SellObject"))
         {
             other.gameObject.SetActive(true);
             count = count + 500;
 
             SetCountText();
             Destroy(gameObject);
         }
     }
 }
Answer by keroltarr · Mar 03 at 01:04 PM
if there is only 1 TextMeshProUGUI object in the game you can assign it in the script with:
 FindObjectOfType<TextMeshProUGUI >();
Thank you! I had this problem for so long so I'm really glad I was able to fix it.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                