Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

TextMesh Pro Changing text through C# script : NullReferenceException

Discussion in 'UGUI & TextMesh Pro' started by JackioSBCC, May 6, 2018.

  1. JackioSBCC

    JackioSBCC

    Joined:
    Dec 27, 2015
    Posts:
    3
    Before you mar this post as a duplicate, I have searched every where for an answer the works, but all give me the same error still.

    I have 4 sliders on my canvas that I am using for health, hunger, thirst and stamina. Over time there values slowly decreases, based on there associated fall rates. I also have TextMeshPro texts associated with each slider and I want the value to change, based on the value of the slider.

    My problem is that when I try to change the text, I get a
    NullReferenceException: Object reference not set to an instance of an object
    . I am not sure why I am recieving this error, and every attempted of resolving this, both using answer from these forums, stackoverflow and youtube, has had no affect.

    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 PlayerStats : MonoBehaviour
    8. {
    9.     // TEXT MESH PRO
    10.     public TMPro.TextMeshProUGUI _healthText;
    11.     public TMPro.TextMeshProUGUI _hungerText;
    12.     public TMPro.TextMeshProUGUI _thirstText;
    13.     public TMPro.TextMeshProUGUI _staminaText;
    14.  
    15.     public float healthValue;
    16.     // HEALTH
    17.     public Slider healthSlider;
    18.     public int maxHealth;
    19.     public int healthFallRate;
    20.  
    21.     // HUNGER
    22.     public Slider hungerSlider;
    23.     public int maxHunger;
    24.     public int hungerFallRate;
    25.  
    26.     // THIRST
    27.     public Slider thirstSlider;
    28.     public int maxThirst;
    29.     public int thirstFallRate;
    30.  
    31.     // STAMINA
    32.     public Slider staminaSlider;
    33.     public int maxStamina;
    34.     public int staminaFallRate;
    35.     public int staminaFallMult;
    36.     private int staminaRegainRate;
    37.     public int staminaRegainMult;
    38.  
    39.     private FPController player;
    40.  
    41.     void Start()
    42.     {
    43.         healthSlider.maxValue = maxHealth;
    44.         healthSlider.value = maxHealth;
    45.      
    46.         hungerSlider.maxValue = maxHunger;
    47.         hungerSlider.value = maxHunger;
    48.      
    49.         thirstSlider.maxValue = maxThirst;
    50.         thirstSlider.value = maxThirst;
    51.  
    52.         staminaSlider.maxValue = maxStamina;
    53.         staminaSlider.value = maxStamina;
    54.  
    55.         staminaFallRate = 1;
    56.         staminaRegainRate = 1;
    57.  
    58.         player = GetComponent<FPController>();
    59.  
    60.         // Here I am trying to access the TextMeshPro object
    61.         _healthText  = GetComponent<TMPro.TextMeshProUGUI>();
    62.         _hungerText  = GetComponent<TMPro.TextMeshProUGUI>();
    63.         _thirstText  = GetComponent<TMPro.TextMeshProUGUI>();
    64.         _staminaText  = GetComponent<TMPro.TextMeshProUGUI>();
    65.     }
    66.  
    67.     void Update()
    68.     {
    69.         // HEALTH CONTROLLER
    70.         if (hungerSlider.value <= 0 && (thirstSlider.value <= 0))
    71.         {
    72.             healthSlider.value -= Time.deltaTime / healthFallRate * 2;
    73.         }
    74.      
    75.         else if (hungerSlider.value <= 0 || (thirstSlider.value <= 0))
    76.         {
    77.             healthSlider.value -= Time.deltaTime / healthFallRate;
    78.         }
    79.      
    80.         if (healthSlider.value <= 0)
    81.         {
    82.             CharacterDeath();
    83.         }
    84.      
    85.         // HUNGER CONTROLLER
    86.         if (hungerSlider.value >= 0)
    87.         {
    88.             hungerSlider.value -= Time.deltaTime / hungerFallRate;
    89.         }
    90.      
    91.         else if (hungerSlider.value <= 0)
    92.         {
    93.             hungerSlider.value = 0;
    94.         }
    95.      
    96.         else if (hungerSlider.value >= maxHunger)
    97.         {
    98.             hungerSlider.value = maxHunger;
    99.         }
    100.      
    101.         // THIRST CONTROLLER
    102.         if (thirstSlider.value >= 0)
    103.         {
    104.             thirstSlider.value -= Time.deltaTime / thirstFallRate;
    105.         }
    106.      
    107.         else if (thirstSlider.value <= 0)
    108.         {
    109.             thirstSlider.value = 0;
    110.         }
    111.      
    112.         else if (thirstSlider.value >= maxThirst)
    113.         {
    114.             thirstSlider.value = maxThirst;
    115.         }
    116.  
    117.         // STAMINA CONTROLLER
    118.  
    119.         if (player.moveSpeed == player.runSpeed)
    120.         {
    121.             staminaSlider.value -= Time.deltaTime / staminaFallRate * staminaFallMult;
    122.         }
    123.  
    124.         else if (player.moveSpeed == player.crchRunSpeed)
    125.         {
    126.             staminaSlider.value -= Time.deltaTime / staminaFallRate;
    127.         }
    128.  
    129.         else
    130.         {
    131.             staminaSlider.value += Time.deltaTime / staminaRegainRate * staminaRegainMult;
    132.         }
    133.  
    134.         if (staminaSlider.value >= maxStamina)
    135.         {
    136.             staminaSlider.value = maxStamina;
    137.         }
    138.  
    139.         else if (staminaSlider.value <= 0)
    140.         {
    141.             staminaSlider.value = 0;
    142.             player.runSpeed = player.walkSpeed;
    143.             player.crchRunSpeed = player.crchSpeed;
    144.             staminaSlider.value += Time.deltaTime / staminaRegainRate * staminaRegainMult;
    145.         }
    146.  
    147.         else if (staminaSlider.value >= 0)
    148.         {
    149.             player.runSpeed = player.runSpeedNorm;
    150.             player.crchRunSpeed = player.crchSpeedNorm;
    151.         }
    152.  
    153.         // Here I am getting the value of the slider, converting it to an int, and then
    154.         // trying to assign that to the text.
    155.         healthValue = (int)healthSlider.value;
    156.         _healthText.text = " / 100";  // This is the line associated with the error
    157.  
    158.     }
    159.  
    160.     void CharacterDeath()
    161.     {
    162.         // KILL PLAYER
    163.     }
    164.  
    165. }
    Any help would be greatly appreciated, as I have been struggling this for the last week.

    Many thanks, Jack.
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    Where specifically does the error show up? What line in this code?

    I'd try checking about line 65 to make sure that _whateverText != null - and if it is, your not getting the component right.
     
  3. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    At line 10 - 13, you have public fields where I presume you assign a reference to the appropriate text component / object in the editor.

    However, at line 61 - 64 you use GetComponent<TextMeshProUGUI>() to get a reference to some text components.

    Since this script is likely attached to some GameObject which does not contain a TextMeshProUGUI component, the GetComponent function correctly returns a Null Reference. In addition, you also end up overriding whatever assignment / reference you had setup in the Editor and those public fields between line 10 - 13.

    When using GetComponent or similar API functions, you have to be mindful of what object they apply to.
     
    Last edited: May 7, 2018
    MD_Reptile likes this.
  4. JackioSBCC

    JackioSBCC

    Joined:
    Dec 27, 2015
    Posts:
    3
    It appears on line 156
     
  5. JackioSBCC

    JackioSBCC

    Joined:
    Dec 27, 2015
    Posts:
    3
    Yeah this script i attached to my player object, and I am trying to access TextMeshPro objects located on the canvas. So what should i use inseatd of GetComponent? Do I need to get a reference to the canvas first, and then use GetComponent of the canvas?
     
  6. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Since I presume you are assigning those public fields between line 10 - 13 in the Editor / Inspector, you don't need the GetComponent<>(); as you already have a reference to those components as a result of the manual assignment.
     
  7. RickkCedoof

    RickkCedoof

    Joined:
    Jul 12, 2019
    Posts:
    1
    The best way to do is
    Code (CSharp):
    1.  
    2. public TextMeshProUGUI health ;
    3. public int lives = 3 ;
    4. health = health.GetComponent<TextMeshProUGUI>() ;
    5. //exemple of using
    6. void update()
    7. {
    8.       health.setText(lives.toString()) ;
    9. }
     
  8. ErgosphereGameStudio

    ErgosphereGameStudio

    Joined:
    Mar 21, 2015
    Posts:
    2
    It worked very nice! Thank you Rickk :D