4
\$\begingroup\$

I am working on a feature that when the Player pass thru a door, a message will popup on the screen.
I'm having few problems: Debug.Log gives me from 2 to 6 logs when I pass thru the collider plane and when I come back to the same collider plane the script it is not been call a second time.

What I'm trying to achive is that Debug.Log gives me only one log and every time I pass thru the collider the script runs again. so here is my code:

function OnTriggerEnter (other : Collider) { //OnTriggerStay gives the same result
    if (other.tag == "Player") {
    isColliding = true;
  } else {
    isColliding = false;
  }
}
function OnGUI() { 
  if (isColliding) {
      Debug.Log(isColliding);
    //Application.ExternalCall('hive.OpenHiveAlert', 'test');
  } 
}
function OnTriggerExit() {
    Destroy(this);
}

an image of the collider plane

Any help would be appreciated, thanks.

\$\endgroup\$
1
\$\begingroup\$

Athos is correct about OnGUI. If you really want to limit it to one Debug.Log message, you can disable the bool in the OnGUI - treating it like a flag that's only true until OnGUI responds the first time. (Though as Athos points out, you'll be fine once you actually start displaying the GUI properly)

Also note that due to the else statement in OnTriggerEnter, if something else were to enter the door trigger, it would disable the isColliding flag which doesn't make a lot of sense to me since the player's collision status doesn't really depend on whether something else enters the doorway

\$\endgroup\$
1
\$\begingroup\$

That happens because rather than calling Debug.Log once your OnTriggerEnter is called, you are calling it inside the OnGUI. OnTriggerEnter is called only once you enter, which means it does only get called once, whereas OnGUI is called (more than) once per frame, unrelated to any such physic events.

As long as your player is inside the collider (because isColliding becomes true once you enter it), you never reset this. Only once you exit it, the object is destroyed and you no longer see any calls. You will probably have your desired result once you start drawing actual text (because as long as the player is inside the collider, it will draw the GUI).

If you want only one Debug.Log, you should instead place this in the body of your if-statement in your OnTriggerEnter method.

\$\endgroup\$
0
\$\begingroup\$

OnTriggerExit Did not work in any posible form, what I did in order to trigger my function only once and "reuse it" again is in the folowing code:

#pragma strict

var isTrigger = false;

function OnTriggerEnter (other : Collider) {
    if (other.tag == "Player") {
        if (!isTrigger) {
            Application.ExternalCall('hive.OpenHiveAlert', 'test');
            isTrigger = true;

            yield WaitForSeconds (10);
            isTrigger = false;
        }
    }
}

It may not be the ideal code, but I can't spend any more time on that... Thank you @Athos and @Jack for your time. :)

\$\endgroup\$
2
  • \$\begingroup\$ Did you find out more about the error? I'm struggling with it right now. \$\endgroup\$
    – FFN
    Nov 23 '17 at 12:02
  • \$\begingroup\$ @FFN Ask a new question with the code you've done so far \$\endgroup\$ Nov 23 '17 at 16:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .