RedditPP Mascot

RedditPP

r/unity • … • u/AlanRizzman

NullReferenceException isn't really making sense

Hi,

I'm a newbie in Unity, only started learning last week because I need to make a small game as part of my internship.

I used canvas for my UI Images, Text and buttons, and I wanted my text to appear letter by letter like in most rpgs.

I watched a guide on how it works, and I tried to do it, but I get a NullReferenceException at a line where it shouldn't be there.

Here is my code (the error is on line 26, but no matter what I write on that line, the error stays on the first line of my coroutine...) :

edit : SOLVED ! Thanks a lot everyone for your help \^\^

using UnityEngine; using TMPro; using System.Collections; //using UnityEngine.UI;

public class TextTyper : MonoBehaviour { string textToType; TMP_Text textComponent; void Awake() { textToType = "Testing"; TMP_Text textComponent = GetComponent<TMP_Text>(); if (textComponent == null) { Debug.LogError("TextTyper: TMP_Text component is not attached to the GameObject. Please attach a TMP_Text component."); } } void Start() { StartCoroutine(TypeText()); }

IEnumerator TypeText() { if (textComponent.text == null) { Debug.LogError("TextTyper: Text component is null. Please ensure the TMP_Text component is attached to the GameObject."); } textComponent.text = "";

foreach (char letter in textToType.ToCharArray()) { textComponent.text += letter; yield return new WaitForSeconds(0.05f); } yield return null; } }

— 0