0

I'm trying to change my text if b1 is triggered.

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using TMPro;
  public class Player_Start : MonoBehaviour
  {
    [SerializeField] public TextMeshProUGUI textTest;
    public void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.gameObject.CompareTag("b1"))
         {
           textTest.text = "Now let's move the right leg up!";                 
           transform.GetChild(0).gameObject.SetActive(false);
           transform.GetChild(1).gameObject.SetActive(true);
        }
        if (collision.gameObject.CompareTag("b2"))
       {
         transform.GetChild(1).gameObject.SetActive(false);
         transform.GetChild(3).gameObject.SetActive(true);
       }
    }
}

I get an error saying

NullReferenceException: Object reference not set to an instance of an object Player_Start.OnTriggerEnter2D (UnityEngine.Collider2D collision) (at Assets/Player_Start.cs:16)

Line 16 says:

textTest.text = "Now let's move the right leg up!"; 
James Z
  • 12,209
  • 10
  • 24
  • 44
JM Nav
  • 43
  • 6

1 Answers1

0

Does it work if you change:

TextMeshProUGUI textTest

to

TMP_Text textTest

(and then re-assign your tmp gameobject to the textTest variable on your script of course)

Otherwise that error often just means you forgot to assign something to that variable in the inspector.

sistm
  • 1
  • 1