Gamebridge/Message Tutorial

From Noisebridge Wiki
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 = "";
               }
       }
}