0

I have this code to detect if the player is in or out of a trigger when I click a button. BUT how can I do to appears a message in the canvas with "Map on"/"Map off" if the player is in/out the trigger? And How can I do it to add a UI button in the canvas instead of the key space?

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 UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class DetectarMapa_BT : MonoBehaviour {

public Button b1;

private bool isTriggerred;

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

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

void OnEnable()
{
    b1.onClick.AddListener (() => buttonCallBack (b1));
}

private void buttonCallBack(Button buttonPressed)
{
    if (buttonPressed == b1) {
        isTriggerred = true;
    }
}

void OnDisable()
{
    b1.onClick.RemoveAllListeners();
}

}
Angelsm
  • 337
  • 5
  • 15
  • You subscribe to Button click events described in **2.Button Component:** from the duplicated answer. Then with [Ludovic's](http://stackoverflow.com/a/42627702/3785314) answer, you can check `isTriggered` variable from his answer is `true` in the button callback function. – Programmer Mar 06 '17 at 17:52
  • I'm going to try it. And how can I do to appears a message in the canvas with "Map on"/"Map off" if the player is in/out the trigger? – Angelsm Mar 06 '17 at 18:43
  • 1
    Create a Text Component then Enable/Disable the Text component or GameObject after trigger is detected? That should be the easiest part of this question.... If you can't do that, create new question with your current Button code and many people here can help you but try before creating a new question. – Programmer Mar 06 '17 at 18:49
  • @Programmer I've tried what you say to me but doesn't work. I suppose I'm not do it fine.. I update my code above. – Angelsm Mar 07 '17 at 14:02
  • You remove "How can I do it to add a UI button in the canvas instead of the key space?" from your question which makes my comments to be useless. Please put that part back to your question. If you paid attention, your updated code is what you should have used to ask your new question but unfortunately, you didn't have "time" to implement that even when the time it takes to do that is < the time it would have taken to ask new question on SO. – Programmer Mar 07 '17 at 14:31
  • I've added the part removed. Thank you for your work and I´m sorry for my mistake. – Angelsm Mar 07 '17 at 16:00

0 Answers0