- Home /
OnTriggerStay
I tried using this in my code and had issues. I would like to understand how to use and apply OnTriggerStay in my script. In the code I'm trying to decrease uses health while on the fire but none of the code runs when conditions are met in game.
function OnTriggerStay (other : Collider) { if (hit.gameObject.tag == "fire") { Debug.Log("hit fire tag"); var healthScript: gui = Camera.main.GetComponent("gui"); if(healthScript.health > 0) { if (healthScript != null){ healthScript.health = healthScript.health -1;
Debug.Log("dying!"); } else if (hit.gameObject.tag == "heal") { var healthScript2: gui = Camera.main.GetComponent("gui"); if(healthScript2.health < 100) { if (healthScript2 != null){ healthScript2.health = healthScript.health +1;
Debug.Log("healing!");} } } } } }
This is the code that I got to work. though I'm still not sure how one would use OnTriggerStay. function OnControllerColliderHit (hit : ControllerColliderHit) {
if (hit.gameObject.tag == "fire")
{
Debug.Log("hit fire tag");
var healthScript: gui = Camera.main.GetComponent("gui");
if(healthScript.health > 0)
{
if (healthScript != null){
healthScript.health = healthScript.health -1;
Debug.Log("dying!");
}
}
}
else if (hit.gameObject.tag == "heal")
{
Debug.Log("hit heal tag");
var healthScript2: gui = Camera.main.GetComponent("gui");
if (healthScript2 != null)
{
if(healthScript2.health < 100)
{
healthScript2.health = healthScript2.health +1;
Debug.Log("healing!");
}
}
}
here's the code that isn't working using OnTriggerStay. The if statement seems to work fine I wouldn't know since the function doesn't run.
function OnTriggerStay (other : Collider) { Debug.Log("OnTriggerStay"); if (other.gameObject.tag == "fire") { Debug.Log("hit fire tag"); var healthScript: gui = Camera.main.GetComponent("gui"); if(healthScript.health > 0) { if (healthScript != null){ healthScript.health = healthScript.health -1; Debug.Log("dying!"); } } } else if (other.gameObject.tag == "heal") { Debug.Log("hit heal tag"); var healthScript2: gui = Camera.main.GetComponent("gui"); if (healthScript2 != null) { if(healthScript2.health < 100) {
healthScript2.health = healthScript2.health +1;
Debug.Log("healing!");
}
}
}
}
Answer by hoy_smallfry · Feb 22, 2013 at 09:16 PM
Have you tried a simple sanity check?
function OnTriggerStay (other : Collider)
{
Debug.Log("OnTriggerStay");
}
It may be the initial if statement. The variable passed into the function, "other", is the Collider that your script's Collider is currently colliding with. If that other Collider is meant to be the fire, then try this if statement instead:
if(other.tag == "fire")
How about the more explicit
if(other.gameObject.tag == "fire")
Answer by Cjen · Feb 23, 2013 at 01:29 AM
Tried that, I get an error message saying Unexpected token .. I know it's the initial statement that doesn't run since the rest of the code runs fine with the function OnControllerColliderHit (hit : ControllerColliderHit){ I ran the check to make sure too.
the other tag is meant to be for fire or heal. The code is suppose to work when character Controller is on a collider of that tag.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
My health decreases to fast. 1 Answer
Operator Cannot Be used with a left hand type 'System.type' and a right hand type 'int' 2 Answers
Can someone write me a health script? 2 1 Answer
Death upon health = 0 2 Answers