Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Cjen · Feb 22, 2013 at 08:23 PM · ontriggerstayhealth

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!");} } } } } }

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cjen · Feb 23, 2013 at 02:12 AM 0
Share

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!");
                 }
             }
         }

}

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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")
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hoy_smallfry · Feb 23, 2013 at 01:33 AM 0
Share

How about the more explicit

 if(other.gameObject.tag == "fire")
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges