Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Resolved Problem when trying to replace TextMeshPro text with code

Discussion in 'UGUI & TextMesh Pro' started by dnlblasco06, Aug 28, 2021.

  1. dnlblasco06

    dnlblasco06

    Joined:
    Jul 3, 2021
    Posts:
    8
    When the code is loaded the variable disappears and this error appears:

    NullReferenceException: Object reference not set to an instance of an object
    LoadingPanelExtern.ChangeUsernameText () (at Assets/Jachi/Scripts/LoadingPanelExtern.cs:56)
    LoadingPanelExtern.Update () (at Assets/Jachi/Scripts/LoadingPanelExtern.cs:40)


    This is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5. using PlayFab;
    6. using PlayFab.ClientModels;
    7.  
    8. public class LoadingPanelExtern : MonoBehaviour
    9. {
    10.  
    11.     [Header("Importante")]
    12.     public GameObject LoadingPanelExtern2;
    13.     public GameObject AccountsPanel;
    14.     public GameObject UI1;
    15.     public GameObject UI2;
    16.     public GameObject UI3;
    17.     public float targetTime = 6.0f;
    18.  
    19.     [Header("Interfaz")]
    20.     public TextMeshProUGUI UsernameTextHome;
    21.     //public TextMeshProUGUI UsernameTextSettings;
    22.     public TextMeshProUGUI UsernameTextRadio;
    23.  
    24.     //string PFUA = GetComponent<PlayFabManager>().PlayFabUserActual;
    25.  
    26.     void Start()
    27.     {
    28.         AccountsPanel.SetActive(false);
    29.         UI1.SetActive(true);
    30.         UI2.SetActive(true);
    31.         UI3.SetActive(true);
    32.     }
    33.     void Update()
    34.     {
    35.         targetTime -= Time.deltaTime;
    36.  
    37.         if (targetTime <= 0.0f)
    38.         {
    39.             timerEnded();
    40.             ChangeUsernameText();
    41.         }
    42.  
    43.     }
    44.  
    45.     void timerEnded()
    46.     {
    47.         LoadingPanelExtern2.SetActive(false);
    48.     }
    49.  
    50.     void ChangeUsernameText()
    51.     {
    52.         UsernameTextHome = GetComponent<TextMeshProUGUI>();
    53.         UsernameTextRadio = GetComponent<TextMeshProUGUI>();
    54.         //UsernameTextSettings = GetComponent<TextMeshProUGUI>();
    55.  
    56.         UsernameTextHome.text = PlayerPrefs.GetString("username");
    57.         UsernameTextRadio.text = PlayerPrefs.GetString("username");
    58.     }
    59. }
    60.  
    VIDEO:
     
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    In your ChangeUsernameText() function, you have UsernameTextHome = GetComponent<TextMeshProUGUI>(); which will try to get a component of type <TextMeshProUGUI> on the game object that contains this script. Since there is no component of that type on that game object is returns null which is why whatever is assigned there disappears.

    Since you have already assigned a reference to these two text components in the Inspector, you do not need to use the GetComponent() function.
     
    dnlblasco06 likes this.
  3. dnlblasco06

    dnlblasco06

    Joined:
    Jul 3, 2021
    Posts:
    8
    thanks, it worked