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 /
This question was closed Nov 22, 2013 at 09:25 PM by primus88 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by primus88 · Nov 21, 2013 at 10:34 PM · collisiontriggerdamage

Why OnTriggerStay applies this action on all colliders

Hello,

I have a collider.tag set on multiple AIs of the same type in the scene. When one of the AI enters a damage OnTriggerStay area, all the AIs in the scene take the damage, not only the triggering AI.

The code:

     void OnTriggerStay(Collider col)
         {
             if(col.tag == "SpiritForm" && counter > 0)
             {
                 theTarget = col.gameObject;
                 RateCount += Time.deltaTime;
                 if (RateCount >= Rate)
                 {
                     if(theTarget != null)
                     {
                     theTarget.SendMessageUpwards("GotHit", Damage, SendMessageOptions.DontRequireReceiver);
                     GameObject newGameObject = (GameObject)Instantiate(prefabToInstantiate, theTarget.transform.position, Quaternion.identity);
                     RateCount = 0.0f;
                     counter -= 1;
                     }
                 }
             
 
 }

Where am I screwing things up?

Comment
Add comment
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

2 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by ThePunisher · Nov 21, 2013 at 10:44 PM

I'm not sure what the hierarchy of the objects looks like but in order to avoid the problem all together you could just do the following to ensure only the GameObject that is inside the collider will get damaged.

 void OnTriggerStay(Collider col)
     {
         if (col.tag == "SpiritForm" && counter > 0)
         {
             theTarget = col.gameObject;
             RateCount += Time.deltaTime;
             if (RateCount >= Rate)
             {
                 if (theTarget != null)
                 {
                     TheComponentWithGoHit comp = theTarget.GetComponent<TheComponentWithGoHit>();
                     comp.GoHit(Damage);
                     GameObject newGameObject = (GameObject)Instantiate(prefabToInstantiate, theTarget.transform.position, Quaternion.identity);
                     RateCount = 0.0f;
                     counter -= 1;
                 }
             }
         }
     }
Comment
Add comment · Show 2 · 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 primus88 · Nov 22, 2013 at 09:25 PM 0
Share

Thanks man, that indeed solved the issue. Will mark you answer now.

avatar image ThePunisher · Nov 22, 2013 at 11:29 PM 0
Share

No problem, glad to help :p

avatar image
1

Answer by aldonaletto · Nov 21, 2013 at 11:13 PM

This smells like a "static variable curse": if the AI health variable is declared as static, all AIs will suffer whenever one of them gets hurt. A static variable pertains to the class, not to the instance, thus all scripts where it's declared share the same variable. If this is the case, remove the static keyword.

Comment
Add comment · Show 4 · 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 ThePunisher · Nov 21, 2013 at 11:25 PM 0
Share

You, sir, have a really good sense of smell :p

avatar image aldonaletto · Nov 22, 2013 at 01:17 AM 0
Share

That's because I've sensed that smell many times before in my own scripts...

avatar image ThePunisher · Nov 22, 2013 at 04:05 AM 0
Share

:D Unfortunately he didn't post the script that has the GoHit method on it :(

avatar image primus88 · Nov 22, 2013 at 08:20 AM 0
Share

Hi guys,

Your idea is good aldonaletto and that was the first thing I double checked(had this issue many times before myself) but it's not the case here. The health variable is not defined as static.

When I send the damage message by other methods, it works just fine. So the problem is really in this method, in the way I define the 'target'.

I will check and get back to you guys. Thank you!

Follow this Question

Answers Answers and Comments

18 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Collision, tags, and triggers 1 Answer

My player HP are get decreased from several GameObjects that has is Trigger on 0 Answers

Error with function OnTriggerXXX 1 Answer

Collision detection accuracy issues. 1 Answer

OnTriggerEnter Being Called On Unexpected Object 1 Answer


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