Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

subtitle show on trigger

Discussion in 'Scripting' started by undyingwill, Jun 26, 2017.

  1. undyingwill

    undyingwill

    Joined:
    Feb 3, 2016
    Posts:
    92
    Hi guys, can this be done using only unity? oO No one archived this as i read whole internet about this specific problem (almost all games are using text pop up on trigger, but no one share how).

    All i need is, when player enter box collider (trigger), text appear for 5-10 seconds and after that time object should be destroyed.

    3d text is more than useless btw, i can see it trought all the walls.
     
  2. Cookieg82

    Cookieg82

    Joined:
    Mar 28, 2017
    Posts:
    52
    public GameObject text;

    public class showtext(){

    void OnTriggerEnter (collider other)
    {

    if (other.gameObject.CompareTag("Player"))

    text.enabled = true;
    }

    void OnTriggerExit (collider other)
    {

    if (other.gameObject.CompareTag("Player"))

    text.enabled = false;

    This just use a simple collider on trigger event to display text. If you really want timed display look into timers and co-routines. It is available if you do a thorough google or YouTube search.

    Happy searching!

    Apply this script to the trigger. Then place whatever text into the inspector.
     
    Last edited: Jun 26, 2017
  3. undyingwill

    undyingwill

    Joined:
    Feb 3, 2016
    Posts:
    92
    Gameobject text what kind of object should be? 3D text or canvas? I should display only one time, and when player leave collider, destroy whole trigger.
     
  4. Cookieg82

    Cookieg82

    Joined:
    Mar 28, 2017
    Posts:
    52
    Hiya! Many apologies - been side tracked. The "Gameobject" can be anything - I would go for Canvas with Text, it is easier for positioning purposes. You can do a destroy on the object if you wish?

    using UnityEngine;

    public class DestroyObject : MonoBehaviour
    {

    public float lifetime = 5; // or 10 seconds //

    void OnTriggerEnter (collider other)
    {

    if (other.gameObject.CompareTag("Player"))

    text.enabled = true;
    }

    void OnTriggerExit (collider other)
    {
    Destroy(gameObject, lifetime);
    }
    }

    Test this out as not sure if there is a syntax error here... untested myself.
     
    Last edited: Jul 19, 2017
  5. Cookieg82

    Cookieg82

    Joined:
    Mar 28, 2017
    Posts:
    52
    P.s. you could add an animation to give the text (if 3D?) a soft bump or hover up/down effect?