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
0
Question by TheShadyColombian · Oct 31, 2014 at 03:34 PM · c#triggerontriggerstay

Call function on all objects in trigger

C#

I want to call the function that is in the OnTriggerStay but it's only calling it for one object at a time. how can i make it so that it calls it for all the game objects at the same time? this will be used for an explosion-type damage.

 using UnityEngine;
 using System.Collections;
 
 public class EnemyDamageZone : MonoBehaviour {
 
     public float AmountOfDamage;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
 
 
     
     }
 
     void OnTriggerStay (Collider ObjectToDamage){
 
         if(ObjectToDamage.gameObject == GameObject.FindGameObjectWithTag("Enemy")){
 
             ObjectToDamage.gameObject.GetComponent<ExperimentalEnemy> ().EnemyDamage (AmountOfDamage);
 
         }
 
     }
 
 }
 
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

· Add your reply
  • Sort: 
avatar image
1

Answer by Wisearn · Oct 31, 2014 at 03:38 PM

When you detonate the explosion, you can put a single:

http://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html

That will give you an array of all objects in the sphere.

For purposes where you need a custom trigger shape, you would have to manually add objects to a list/array at OnTriggerEnter and remove objects from the list at OnTriggerExit and on detonation call the function on all the objects in the list but this can be a bit risky from my experience.

Comment
Add comment · Show 5 · 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 drod7425 · Oct 31, 2014 at 03:43 PM 0
Share

This is what I was trying to think of, but my brain only returned SphereCastAll. OverlapSphere gives less precise info (such as where the hit was), but you probably don't need to be that precise when doing an explosion. I like this answer better.

avatar image Wisearn · Oct 31, 2014 at 03:55 PM 0
Share

Does SphereCastAll return the closest hit point from origin?

That would be useful, more useful than just comparing this.transform.position to hitColliders[i].transform.position

But I imagine OverlapSphere being less harsh on performance

avatar image drod7425 · Oct 31, 2014 at 06:52 PM 0
Share

You can get hit.distance from each, but you'd still need to use $$anonymous$$athf.$$anonymous$$in to get the closest one.

avatar image TheShadyColombian · Nov 01, 2014 at 02:40 AM 0
Share

the Unity Docs example works perfectly to the point where I do a Debug.Log (hitColliders[i]) but then when i add a line that says hitColliders.gameObject.GetComponent().nameOfTheFunction; and it gives me an error that it couldn't be found

avatar image Wisearn · Nov 03, 2014 at 10:38 AM 0
Share

You'd have to do a loop over hitColliders.Count/length and call nameOfTheFunction on each hitColliders[0], hitColliders[1] et.c.

Just like the example shows:

 int i = 0;
 while (i < hitColliders.Length) {
     //here you go ins$$anonymous$$d of sendmessage
     hitColliders[i].gameObject.GetComponent<Name>().FunctionName();
     i++;
 }
avatar image
0

Answer by drod7425 · Oct 31, 2014 at 03:38 PM

You'll want to use Physics.SphereCastAll: http://docs.unity3d.com/ScriptReference/Physics.SphereCastAll.html

It returns every hit within a radius and you can use that to get the ExperimentalEnemy component and apply damage for each.

Comment
Add comment · Show 3 · 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 TheShadyColombian · Oct 31, 2014 at 03:58 PM 0
Share

if it's an array, how would i call the function to all

avatar image drod7425 · Oct 31, 2014 at 06:45 PM 0
Share

You would loop through the returned array and call the function on each:

 foreach(RayCastHit hit in Physics.SphereCastAll(etc...)){
 hit.transform.gameObject.GetComponent<ExperimentalEnemy>().EnemyDamage (AmountOfDamage);
 }
avatar image TheShadyColombian · Nov 01, 2014 at 02:41 AM 0
Share

is foreach a C# function?

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

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

Related Questions

Want health to decrement longer stays on trigger 2 Answers

Work around for onTriggerStay? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

What is the difference between these two scripts? 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