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 :
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;
}
}

textMeshProvariable anywhere? \$\endgroup\$