1

How can I add a UI Text when the trigger is detected? I have this code to detect if the player is in or out of a trigger BUT I want to appears a message in the canvas with "Map on"/"Map off" if the player is in/out the trigger. Thanks!

public class MapDetect : MonoBehaviour {

private bool isTriggered;

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag("Player"))
        isTriggered = true;
}

void OnTriggerExit(Collider other)
{
    if (other.gameObject.CompareTag("Player"))
        isTriggered = false;
}

void Update(){
    if(Input.GetKey(KeyCode.Space)){
        Debug.Log(isTriggered);
    }
}
}

UPDATE CODE:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DetectarMapa : MonoBehaviour {

public GameObject T1;
public GameObject T2;

public float time = 3;

void Start ()
{
    T1.SetActive (true);
    StartCoroutine(Message1());
}

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag("Player"))
        T2.SetActive (true);
        StartCoroutine(Message2());
}

void OnTriggerExit(Collider other)
{
    if (other.gameObject.CompareTag("Player"))
        T1.SetActive (true);
        StartCoroutine(Message1());
}

IEnumerator Message1 ()
{
    yield return new WaitForSeconds(time);
    T1.SetActive (false);
}

IEnumerator Message2 ()
{
    yield return new WaitForSeconds(time);
    T2.SetActive (false);
}
}
Angelsm
  • 337
  • 5
  • 15
  • 1
    You didn't even try. You couldn't implement the click button [part](http://stackoverflow.com/q/42632196/3785314)? It's good to ask and get answer but also good to show you have tried something. Otherwise, you are hoping for people to write your game script from beginning to end. – Programmer Mar 06 '17 at 19:54
  • Sorry! Now, I have no time to implement the click button part. When I try it I'll write here. – Angelsm Mar 06 '17 at 20:03

1 Answers1

1

Simply add a reference to your Text Component and set the text property when needed:

public class MapDetect : MonoBehaviour {

    public  Text Text;

    void Start()
    {
        Text.text = "Map out";
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
            Text.text = "Map on";
    }

    void OnTriggerExit(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
            Text.text = "Map out";
    }
}

Then in Unity simply add the reference to the Text component.

Ludovic Feltz
  • 11,416
  • 4
  • 47
  • 63
  • When the player is into the trigger the text appears but when he is out doesn't appear – Angelsm Mar 06 '17 at 20:12
  • I've checked when the player enters or exits the trigger the messages appears but if he begin out of the trigger message doesn't appear. How can I solve? – Angelsm Mar 07 '17 at 08:17
  • @Angelsm I do not understand your problem. You mean that the method `OnTriggerExit` is never called? – Ludovic Feltz Mar 07 '17 at 09:26
  • OnTriggerExit works when the player is into the trigger and he exits the trigger, but when the player is out the trigger (and before he has not been into the trigger) doesn't work. For example at the start of the game. – Angelsm Mar 07 '17 at 09:40
  • @Angelsm Simply set the default value of your `Text` *Component* to **"Map out"** in the unity editor. Or you can set it in the `Start()` method of your class... I made an edit with the second solution... – Ludovic Feltz Mar 07 '17 at 09:47
  • Yes, It works. I set it in the Start, but It obviously appears only once if the player is out of the trigger. Really, I have the trigger in an object that is enabled when I click in a button. So only the first time I click in this button the message of the OnTriggerExit appears. How can I do the message appears each time that I click in the button? – Angelsm Mar 07 '17 at 10:51
  • @Angelsm Set the text in the button event ? – Ludovic Feltz Mar 07 '17 at 10:57
  • I've updated above my last code. No, If I do it, always will enable this text, but I want to detect if the player is in or out the trigger when I click the button. – Angelsm Mar 07 '17 at 11:14