Gamebridge/Message Tutorial

From Noisebridge Wiki
Revision as of 04:44, 10 February 2026 by Maintenance script (talk | contribs) (Imported from Noisebridge wiki backup)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

You can place message triggers to display text messages on the screen when the player enters an area.

using UnityEngine;

using System.Collections;
using UnityEngine.UI;

public class Message : MonoBehaviour {

       public Text messageText;
       public string messageString;

       public float startTimeInSeconds = 0f;
       public float timeBeforeMessageVanishes = 5f;
       public static float timeWhenMessageVanishes = 0f;

       void Start()
       {
           messageText = GameObject.Find("Message").GetComponent<Text>();
           messageText.text = "";
       }
       void OnTriggerEnter( Collider other ) {
           if ( other.tag == "Player")
               {
               messageText.text = messageString;
               startTimeInSeconds = Time.time;
               timeWhenMessageVanishes = startTimeInSeconds + timeBeforeMessageVanishes;
           }
       }

       void Update ()
       {
               if ( Time.time >= timeWhenMessageVanishes )
               {
                   messageText.text = "";
               }
       }
}