UI Object Variable Boogaloo

Hello peoples. I’ve run into a snafu. I’ve only very recently begun scripting with the new UI in 4.6 and I’m confused by the text. I am attempting to change the UI text when the first person controller enters a collision zone. As it stand right now, the collision triggers (verified by debug log message) but the UI text does not change. I have a UI canvas with a text object attached to it. I have this script attached to it in order to make a global text variable to be edited by other scripts. using UnityEngine; using UnityEngine.UI; using System.Collections;

  • usingUnityEngine;
  • usingUnityEngine.UI;
  • usingSystem.Collections;
  • publicclass TEXTMAKER :MonoBehaviour{
  • publicTextGametext;
  • voidStart(){
  • Gametext=GetComponent();
  • }
  • }

In addition, I have a second script attached to the collider that is supposed to change the UI text when triggered:

  • usingUnityEngine;
  • usingUnityEngine.UI;
  • usingSystem.Collections;
  • publicclassTriggertextchange:MonoBehaviour
  • {
  • publicTextGametext;
  • voidOnTriggerEnter(Collider other)
  • {
  • Debug.Log(“TRIGGER!”);
  • Gametext= gameObject.GetComponent();
  • Gametext.text=“MAXIMUM TRIGGER.”;
  • }
  • }

When the trigger occurs, this error message pops up in debug: NullReferenceException: Object reference not set to an instance of an object Triggertextchange.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Standard Assets/Character Controllers/Sources/Scripts/Triggertextchange.cs:13)

I am comfortable with C#, but I just can’t wrap my head around this / am probably very dumb. Any help you wonderful people can provide would be greatly appreciated.

Quick update to this gem of a brainbender. I have also now attempted the same in java, something I’m much more comfortable in, and still no results. I understand that it’s an object reference problem, but I can’t find any useful info in the documentation. Please have an annoyed monkey for getting this far.1922694--124146--1421187228441.jpg

First of all, please use script tags correctly.

Secondly, why does your first script assign the gametext object if it isn’t using it?

Assuming the second script is placed on the UI Canvas with the trigger, it would need to find the text in it’s child transform, not it’s own transform.

Additionally, you don’t need to assign the text object every trigger.

Refactored:

using UnityEngine;
using UnityEngine.UI;

public class TriggerTextChange : MonoBehaviour {

    public Text gameText;
   
    void Start() {
       
        //Assuming the child object to this ui element is named "Text"
        //and has a Text ui component
        gameText = transform.Find("Text").GetComponent<Text>();
    }
   
    void OnTriggerEnter(Collider other) {
   
        gameText.text = "Triggered.";
       
    }

}

Thank you kindly, sir. I have now straightened that whole mess out!