3

I’ve placed in the scene an object with a trigger and I want the console sends me a message detecting if the player is in or out of the trigger when I click a button . When I play, it only sends me a message when the player is into the trigger.

Code:

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

public class MapDetect : MonoBehaviour {


     void OnTriggerStay(Collider other)
     {
         if (other.gameObject.tag == "Player") {
             Debug.Log ("Map ON");

         } 
         else {
             if (other.gameObject.tag == "Player") {
                  Debug.Log ("Map OFF");
             }
         }
     }
}
Koby Douek
  • 16,156
  • 19
  • 74
  • 103
Angelsm
  • 337
  • 5
  • 15
  • 2
    Your conditions are identical. If the second is true, then the first will also be true, meaning the second will never run due to the nature of if/else statements. – Seth Flowers Mar 06 '17 at 14:04

3 Answers3

4

Use OnTriggerEnter and OnTriggerExit instead of OnTriggerStay to keep the current state:

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);
        }
    }
}
Ludovic Feltz
  • 11,416
  • 4
  • 47
  • 63
  • 1
    This should do it. Although use, [`gameObject.CompareTag`](https://docs.unity3d.com/ScriptReference/GameObject.CompareTag.html) isntead of `gameObject.tag==`. – Programmer Mar 06 '17 at 14:11
  • 1
    Also worth noting: since you're only interested in the player, you could put your trigger on a layer that _only_ collides with the player (assuming the player is also on such a layer). That way, you don't need the tag check and the physics calculations become cheaper. – Dan Puzey Mar 06 '17 at 15:30
  • Thank you so much @Ludovic. But how can I do if the button is an UI button of the canvas instead of the key space? And I want to appears a message in the canvas with "Map on"/"Map off". – Angelsm Mar 06 '17 at 17:09
  • @Angelsm That's another question, it would be too long to answer in the comments. Ask another question and post the link here so i can try to answer :) – Ludovic Feltz Mar 06 '17 at 17:21
  • Ok, I'm going to do another question. – Angelsm Mar 06 '17 at 17:31
  • @Ludovic Here is the new question http://stackoverflow.com/questions/42632196/how-add-a-message-in-the-canvas-if-the-player-is-in-out-a-trigger – Angelsm Mar 06 '17 at 17:44
0

Your logic is totally wrong. You're only checking if the TRIGGER STAYS IN YOUR BOUNDS but still trying to log "Map OFF" message which will never happen.

Instead of OnTriggerStar method use OnTriggerEnter and OnTriggerExit. Then print the message only when needed ( or in debug mode ) :

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

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

void Update()
{
#if DEBUG
    if ( m_IsPlayerOnTheMap )
    {
        Debug.Log("Map ON");
    }
    else
    {
        Debug.Log("Map OFF");
    }
#endif
}

private bool m_IsPlayerOnTheMap = false;
mrogal.ski
  • 5,828
  • 1
  • 21
  • 30
0

Try:

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

public class MapDetect : MonoBehaviour {


 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.CompareTag("Player"))
     {
         Debug.Log ("Map ON");
     } 
 }

 void OnTriggerExit(Collider other)
 {
     if (other.gameObject.CompareTag("Player"))
     {
         Debug.Log ("Map OFF");
     }
 }
}

This will switch it on when you enter and off when you exit (althout all it does right now is print the result).

Hope it helps.

Mr.Bigglesworth
  • 924
  • 9
  • 22