0
\$\begingroup\$

At the top of the screen :

public GameObject uiSceneText;
private TextMeshPro textMeshPro;

Then in the script at some point :

uiSceneText.SetActive(true);

if (textMeshPro.text != "")
    textMeshPro.text = "";

The exception error null is on the textMeshPro on the line :

if (textMeshPro.text != "")

textMeshPro is null.

This screenshot shows the TextMeshPro in the Hierarchy :

TextMeshPro

and I see that the Scene Image object and its child Scene Text both are enabled true.

I want to get the text and add replace a new text with the text already in the Text but the textMeshPro is null.

The complete script :

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class PlayerSpaceshipAreaColliding : MonoBehaviour
{
    public float rotationSpeed;
    public float movingSpeed;
    public float secondsToRotate;
    public GameObject uiSceneText;

    private float timeElapsed = 0;
    private float lerpDuration = 3;
    private float startValue = 1;
    private float endValue = 0;
    private float valueToLerp = 0;
    private Animator playerAnimator;
    private bool exitSpaceShipSurroundingArea = false;
    private bool slowd = true;
    private TextMeshPro textMeshPro;

    // Start is called before the first frame update
    void Start()
    {
        textMeshPro = uiSceneText.GetComponent<TextMeshPro>();

        playerAnimator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (exitSpaceShipSurroundingArea)
        {
            if (slowd)
                SlowDown();

            if (playerAnimator.GetFloat("Forward") == 0)
            {
                slowd = false;

                LockController.PlayerLockState(false);

                uiSceneText.SetActive(true);

                if (textMeshPro.text != "")
                    textMeshPro.text = "";

                textMeshPro.text = "Here I will add the new text.";
                
            }

            if (slowd == false)
            {
                
            }
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.name == "CrashLandedShipUpDown")
        {
            exitSpaceShipSurroundingArea = false;
            Debug.Log("Entered Spaceship Area !");
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.name == "CrashLandedShipUpDown")
        {
            exitSpaceShipSurroundingArea = true;
            Debug.Log("Exited Spaceship Area !");
        }
    }

    private void SlowDown()
    {
        if (timeElapsed < lerpDuration)
        {
            valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
            playerAnimator.SetFloat("Forward", valueToLerp);
            timeElapsed += Time.deltaTime;
        }

        playerAnimator.SetFloat("Forward", valueToLerp);
        valueToLerp = 0;
    }
}
\$\endgroup\$
5
  • \$\begingroup\$ Do you assign a value to the textMeshPro variable anywhere? \$\endgroup\$ Commented May 25, 2021 at 22:22
  • \$\begingroup\$ @Kevin I forgot to mention that in the Start I have this line textMeshPro = uiSceneText.GetComponent<TextMeshPro>(); \$\endgroup\$ Commented May 25, 2021 at 22:48
  • \$\begingroup\$ Please share the whole script so we can better help you. You've only shared a few lines of code out of context, which isn't enough for us to give you much guidance on what might be wrong. \$\endgroup\$ Commented May 25, 2021 at 22:50
  • \$\begingroup\$ @Kevin I found the problem. In the Start I'm doing textMeshPro = uiSceneText.GetComponent<TextMeshPro>(); but uiSceneText is not yet enabled true. So I guess the solution will be to add a public reference to the TextMeshPro instead of trying to get it in the Start. \$\endgroup\$ Commented May 25, 2021 at 22:51
  • 1
    \$\begingroup\$ Great! Please post your solution as an answer and mark it as the correct answer for future readers. \$\endgroup\$ Commented May 25, 2021 at 22:52

1 Answer 1

0
\$\begingroup\$

First problem: I tried to get the TextMeshPro component in the Start(), but the uiSceneText object was not yet enabled true. That's why I got the null exception error.

Second problem: It's not TextMeshPro but TextMeshProUGUI.

Solution: Instead of trying to get the TextMeshProUGUI component in the Start() I added a new variable for the TextMeshProUGUI and assigned the TextMeshProUGUI in the editor it's working fine now.

The complete working script :

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class PlayerSpaceshipAreaColliding : MonoBehaviour
{
    public float rotationSpeed;
    public float movingSpeed;
    public float secondsToRotate;
    public GameObject uiSceneText;
    public TextMeshProUGUI textMeshProUGUI;

    private float timeElapsed = 0;
    private float lerpDuration = 3;
    private float startValue = 1;
    private float endValue = 0;
    private float valueToLerp = 0;
    private Animator playerAnimator;
    private bool exitSpaceShipSurroundingArea = false;
    private bool slowd = true;

    // Start is called before the first frame update
    void Start()
    {
        playerAnimator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (exitSpaceShipSurroundingArea)
        {
            if (slowd)
                SlowDown();

            if (playerAnimator.GetFloat("Forward") == 0)
            {
                slowd = false;

                LockController.PlayerLockState(false);

                uiSceneText.SetActive(true);

                if (textMeshProUGUI.text != "")
                    textMeshProUGUI.text = "";

                textMeshProUGUI.text = "Some new text will be added here soon.";
                
            }

            if (slowd == false)
            {
                
            }
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.name == "CrashLandedShipUpDown")
        {
            exitSpaceShipSurroundingArea = false;
            Debug.Log("Entered Spaceship Area !");
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.name == "CrashLandedShipUpDown")
        {
            exitSpaceShipSurroundingArea = true;
            Debug.Log("Exited Spaceship Area !");
        }
    }

    private void SlowDown()
    {
        if (timeElapsed < lerpDuration)
        {
            valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
            playerAnimator.SetFloat("Forward", valueToLerp);
            timeElapsed += Time.deltaTime;
        }

        playerAnimator.SetFloat("Forward", valueToLerp);
        valueToLerp = 0;
    }
}
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.