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. Dismiss Notice

Question Help with Looping Error in Script

Discussion in 'Scripting' started by Flapman, May 16, 2023.

  1. Flapman

    Flapman

    Joined:
    Aug 15, 2010
    Posts:
    84
    Hi All,

    I am getting this error and can't quite figure out what is missing in the script. I am sure it something really simple but it is just escaping me. Thanks in advance.

    Error:
    NullReferenceException: Object reference not set to an instance of an object
    CustomScript.Update () (at Assets/CustomScripts/CustomScript.cs:28)


    Code (CSharp):
    1. using Atavism;
    2. using System.Collections.Generic;
    3. using TMPro;
    4. using UnityEngine;
    5. public class CustomScript : MonoBehaviour
    6. {
    7.     [SerializeField] TextMeshProUGUI m_DialogueText;
    8.     public List<TextMeshProUGUI> m_List;
    9.  
    10.     void Start()
    11.     {
    12.         //AtavismEventSystem.RegisterEvent("PROPERTY_" + prop, this);
    13.         //AtavismEventSystem.RegisterEvent("PROPERTY_" + prop_max, this);
    14.         //value = (int)ClientAPI.GetPlayerObject().GetProperty(prop);
    15.         //valueMax = (int)ClientAPI.GetPlayerObject().GetProperty(prop_max);
    16.  
    17.        m_DialogueText = GameObject.Find("DialogueText").GetComponent<TextMeshProUGUI>();
    18.  
    19.     }
    20.  
    21.     [System.Obsolete]
    22.     void Update()
    23.     {
    24.         //Debug.Log(ClientAPI.GetPlayerObject().Name);
    25.         //Debug.Log("Health: " + value);
    26.         //Debug.Log("Max Health: " + valueMax);
    27.  
    28.         if (m_DialogueText.text.Contains("%PlayerName%"))
    29.         {
    30.             string tmp = m_DialogueText.text;
    31.             m_DialogueText.text = tmp.Replace("%PlayerName%", "<#32a885>" + ClientAPI.GetPlayerObject().Name + "</color>");
    32.         }
    33.  
    34.         foreach (var m in m_List)
    35.         {
    36.             if (m.text.Contains("%PlayerName%"))
    37.             {
    38.                 string tmp = m.text;
    39.                 m.text = tmp.Replace
    40.         }
    41.         }
    42.  
    43.     }
    44. }
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,854
  3. Flapman

    Flapman

    Joined:
    Aug 15, 2010
    Posts:
    84
    I am actually a noob at scripting. It is likely that m_DialogueText but how would I set it to the value it is looking for? I am actually trying to figure out how to fix a script someone else made. :)
     
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,124
    Since it's marked with [SerializeField] it's likely supposed to be hooked up in the Inspector.
     
  5. Flapman

    Flapman

    Joined:
    Aug 15, 2010
    Posts:
    84
    Hiyas, actually that was just me trying to make it work from tidbits I am reading online. All I am trying to do is pull both instances of
    public TextMeshProUGUI m_DialogueText;
    public List<TextMeshProUGUI> m_List;

    where they contain %PlayerName% and replace them with the content of the variable "Name"

    I hope that makes sense. Sorry, I am just learning.
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,854
    Well one, double check that it is with some Debug.Log statements. Once you're sure what is null, you need to work out why it's ending up as null.

    Right now you're doing two things in the one line:
    Code (CSharp):
    1. m_DialogueText = GameObject.Find("DialogueText").GetComponent<TextMeshProUGUI>();
    Either of these could return null. You can separate this out into two separate lines of code, with some validation:
    Code (CSharp):
    1. GameObject dialogueGO = GameObject.Find("DialogueText");
    2. if (!dialogueGO)
    3. {
    4.     Debug.LogError("Could Not find DialogueText game object", this);
    5.  
    6. }
    7. else if (!dialogueGO.TryGetComponent<TextMeshProUGUI>(out dialogueText))
    8. {
    9.     Debug.LogError("Dialogue Text Game object does not have a TextMeshProUGUI component!", this);
    10. }
    However, as noted above, you should just assign the references instead by dragging and dropping the references via the inspector, and omit this code entirely.
     
    Flapman likes this.
  7. Flapman

    Flapman

    Joined:
    Aug 15, 2010
    Posts:
    84
    Thanks. Let me try that.
     
  8. Flapman

    Flapman

    Joined:
    Aug 15, 2010
    Posts:
    84
    Hiyas, turns out that the solution was easy. Line 7 and 8 both needed to be serialized and have the objects attached.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Of course, because it's always the same, ALWAYS!

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that

    Just to keep you out of trouble in the future:

    Remember the first rule of GameObject.Find():

    Do not use GameObject.Find();

    More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

    More information: https://forum.unity.com/threads/why-cant-i-find-the-other-objects.1360192/#post-8581066