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 Unity won't let me assign text mesh pro to public text

Discussion in 'Scripting' started by Jordan462, Jan 26, 2022.

  1. Jordan462

    Jordan462

    Joined:
    Nov 22, 2020
    Posts:
    3
    I previously made a code for collectible items and tried doing the same thing here but for some reason, in inspector, I cannot assign a text mesh pro or anything to the public TextMeshProUGUI or the winTextObject even though I could in the collectibles code.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class destroyOnContactBlank : MonoBehaviour
    7. {
    8.     //declare our list of strings we'll use if we want our projectile to destroy an object it hits
    9.     public List<string> destroyableObjects = new List<string>();
    10.     public GameObject explosionEffect;
    11.     public TextMeshProUGUI countText;
    12.     public GameObject winTextObject;
    13.     private int count;
    14.  
    15.     void Start()
    16.     {
    17.         // Set the count to zero
    18.         count = 0;
    19.  
    20.         SetCountText();
    21.  
    22.         // Set the text property of the Win Text UI to an empty string, making the 'You Win' (game over message) blank
    23.         winTextObject.SetActive(false);
    24.     }
    25.     private void OnCollisionEnter(Collision collision)
    26.     {
    27.         //run through our list of tags we want to destroy
    28.         for (int i = 0; i < destroyableObjects.Count; i++)
    29.         {
    30.             //check if the hit object has one of our tags
    31.             if(collision.gameObject.tag == destroyableObjects[i])
    32.             {
    33.                 //destroy the hit object and the projectile
    34.                 // Add one to the score variable 'count'
    35.                 count = count + 1;
    36.  
    37.                 // Run the 'SetCountText()' function (see below)
    38.                 SetCountText();
    39.                 Destroy(gameObject);
    40.                 Destroy(collision.gameObject);
    41.                 GameObject explosion = Instantiate(explosionEffect, transform.position, transform.rotation);
    42.                 Destroy(explosion, 5);
    43.             }
    44.         }
    45.     }
    46.     void SetCountText()
    47.     {
    48.         countText.text = "Count: " + count.ToString();
    49.  
    50.         if (count >= 12)
    51.         {
    52.             // Set the text value of your 'winText'
    53.             winTextObject.SetActive(true);
    54.         }
    55.     }
    56. }
    57.  
     
  2. Jordan462

    Jordan462

    Joined:
    Nov 22, 2020
    Posts:
    3
    This is the collectibles code and it works fine.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5. using TMPro;
    6.     // Create public variables for player speed, and for the Text UI game objects
    7.  
    8.     public class PlayerController : MonoBehaviour
    9. {
    10.     // Create public variables for player speed, and for the Text UI game objects
    11.     public float speed;
    12.     public TextMeshProUGUI countText;
    13.     public GameObject winTextObject;
    14.     public GameObject ball;
    15.  
    16.     private float movementX;
    17.     private float movementY;
    18.  
    19.     private Rigidbody rb;
    20.     private int count;
    21.  
    22.     // At the start of the game..
    23.     void Start()
    24.     {
    25.         // Assign the Rigidbody component to our private rb variable
    26.         rb = GetComponent<Rigidbody>();
    27.  
    28.         // Set the count to zero
    29.         count = 0;
    30.  
    31.         SetCountText();
    32.  
    33.         // Set the text property of the Win Text UI to an empty string, making the 'You Win' (game over message) blank
    34.         winTextObject.SetActive(false);
    35.     }
    36.     private void Update()
    37.     {
    38.         if (transform.position.y < -5)
    39.         {
    40.             ball.transform.position = Vector3.zero;
    41.         }
    42.     }
    43.     void FixedUpdate()
    44.     {
    45.         // Create a Vector3 variable, and assign X and Z to feature the horizontal and vertical float variables above
    46.         Vector3 movement = new Vector3(movementX, 0.0f, movementY);
    47.  
    48.         rb.AddForce(movement * speed);
    49.     }
    50.  
    51.     void OnTriggerEnter(Collider other)
    52.     {
    53.         // ..and if the GameObject you intersect has the tag 'Pick Up' assigned to it..
    54.         if (other.gameObject.CompareTag("PickUp"))
    55.         {
    56.             other.gameObject.SetActive(false);
    57.  
    58.             // Add one to the score variable 'count'
    59.             count = count + 1;
    60.  
    61.             // Run the 'SetCountText()' function (see below)
    62.             SetCountText();
    63.         }
    64.         else if (other.gameObject.CompareTag("Gate") && count >= 12)
    65.         {
    66.             other.gameObject.SetActive(false);
    67.         }
    68.     }
    69.     void OnMove(InputValue value)
    70.     {
    71.         Vector2 v = value.Get<Vector2>();
    72.  
    73.         movementX = v.x;
    74.         movementY = v.y;
    75.     }
    76.  
    77.     void SetCountText()
    78.     {
    79.         countText.text = "Count: " + count.ToString();
    80.  
    81.         if (count >= 12)
    82.         {
    83.             // Set the text value of your 'winText'
    84.             winTextObject.SetActive(true);
    85.         }
    86.     }
    87. }
     
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    But they show up in the inspector and everything else looks normal? If that's the case, then you're probably trying to create an invalid connection like dragging a scene reference into a slot on a prefab. Post some screenshots of your inspector and project.
     
  4. Jordan462

    Jordan462

    Joined:
    Nov 22, 2020
    Posts:
    3
    Yeah, that seems to be the issue. I was trying to drag text in the science onto a prefab for a projectile that destroys enemies. I'll have to find another way to track destroyed enemies. Thank you for your clarifcation.