Gamebridge/Message Tutorial

From Noisebridge
Revision as of 21:14, 23 June 2016 by Lxpk (talk | contribs) (Created page with "You can place message triggers to display text messages on the screen when the player enters an area. <code> using UnityEngine; using System.Collections; using UnityEngine....")
(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 = "";
               }
       }
}