Search Unity

Question TextMeshPro get "NullReferenceException: Object reference not set to an instance of an object"

Discussion in 'UGUI & TextMesh Pro' started by hieuneko, Jul 16, 2022.

  1. hieuneko

    hieuneko

    Joined:
    Jul 11, 2022
    Posts:
    1
    Hello everyone. I'm using Unity 2021.3.6f to design games. Currently, I am doing title when switching tilemap with textmeshpro. I want when the player enters another room, the name of tilemap will appears. But when I come to new room, the fail NullReferenceException: Object reference not set to an instance of an object appears, althought I registered object TextMeshPro. I find and tried all things I get in the internet, like changing the variable declaration type of textmesh or create empty variable,... But it's not working. TextMeshPro the editor I'm using is TextMeshProUGUI.

    Here is my simple code at now.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class RoomMove : MonoBehaviour
    8. {
    9.     public Vector2 cameraChange; //for camera and player position
    10.     public Vector3 playerChange;
    11.     private CameraMovement cam;
    12.     public bool needText;
    13.     public string placeName;  //Name of room
    14.     public GameObject text;
    15.     public TMPro.TextMeshProUGUI placeText;
    16.  
    17.     void Start()
    18.     {
    19.         cam = Camera.main.GetComponent<CameraMovement>();
    20.         placeText = gameObject.GetComponent<TextMeshProUGUI>();
    21.     }
    22.     void Update()
    23.     {
    24.    
    25.     }
    26.     private void OnTriggerEnter2D(Collider2D other)
    27.     {
    28.         if (other.CompareTag("Player"))
    29.         {
    30.             cam.minPosition += cameraChange;
    31.             cam.maxPosition += cameraChange;
    32.             other.transform.position += playerChange;
    33.             if (needText)
    34.             {
    35.                 StopCoroutine(placeNameCo());
    36.                 StartCoroutine(placeNameCo());
    37.             }
    38.         }
    39.     }
    40.     // Set active when enter room and display TextMeshPro
    41.     private IEnumerator placeNameCo()
    42.     {
    43.         text.SetActive(true);
    44.         placeText.text = placeName; //error here
    45.         yield return new WaitForSeconds(2f);
    46.         text.SetActive(false);
    47.     }
    48. }
    The error is "NullReferenceException: Object reference not set to an instance of an object
    RoomMove+<placeNameCo>d__10.MoveNext() (at Assets/Scripts/RoomMove.cs:44)"
    If you solve it, if you don't mind can you tell me how to get the name of the room player is in, i use tilemap. Thank you very much
     
    Last edited: Jul 17, 2022
    Bobby_Boy1322 likes this.
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,861
    Wrong sub forum, but your GetComponent probably isn't finding the TMPro text component. You haven't put a regular text component on by mistake have you?

    Also these are the sorts of values you should just set in the inspector, rather than use GetComponent.
     
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    I respectfully disagree, depending on circumstances. We're talking about a negligible cost at startup time, compared to making someone in your team set up that reference by hand on potentially many occasions, and related errors if one is ever missed.

    As a general rule I'll get a computer to do all work where it's reasonable to do so, and only make a human hook up a reference if it's meant to be configurable or optional.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,861
    I mean, fair cop. I've never worked in a game dev team, and I imagine a lot of new users here never have either so not something we tend to think about. ;p

    Though I do forget RequireComponent exists, which I guess is quite suitable in situations like this one.
     
    angrypenguin likes this.
  5. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723