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 Bolhuis · May 25, 2013 at 04:22 PM · gameobjecttagsendmessage

SendMessage to all *tag* within range

I have some code for finding the nearest gameObject with the tag "Enemy" - checkEnemy(). I also have a function to SendMessage to the closest enemy. But I'd like to target all objects with the Enemy tag, withing the distance of 8, instead of just the nearest one. I hope that explains it properly.

 function checkEnemy()
 {
     Enemy = GameObject.FindGameObjectWithTag("Enemy");
     
     Enemies = GameObject.FindGameObjectsWithTag("Enemy"); 
     
     var closestDist = Mathf.Infinity; 
  
     for (Enemy in Enemies) { 
         var dist = (transform.position - Enemy.transform.position).sqrMagnitude; 
  
         if (dist < closestDist) { 
             closestDist = dist; 
             closest = Enemy;
             } 
     } 
     if (closest)
     {
         distance = Vector3.Distance(closest.transform.position, Player.transform.position);
     }
     

---------------------------------- ApplyDamage part

 if (Weapon03.active == true)
 {
 
 if (particle.isPlaying == false)
 
 {
         particle.transform.position = particleLocation.transform.position;
 
         particle.Play();
 
 if (distance < 8)
 
     {
              closest.SendMessage("ApplyDamage", 100);
     }
   }            
 
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Vonni · May 25, 2013 at 05:10 PM

Did you write this?

Couldnt you just do almost what your doing like this?

 pseudo code:
 for(enemy in enemyList)
    get distance(player enemy)
    if(distance is < 8)
      enemy.SendMessage("ApplyDamage", 100);

NB: Dont use capital letters for variables (Enemy, Enemies etc..) it looks like a type when you read it and is annoying :)

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 Bolhuis · May 25, 2013 at 06:05 PM 1
Share

Nailed it, thanks for the response!

Here's the code if anyone should be interested:

 function aoEExplosion ()
 {
     
     enemy = GameObject.FindGameObjectWithTag("Enemy");
     enemies = GameObject.FindGameObjectsWithTag("Enemy");
     var closestDist = $$anonymous$$athf.Infinity;
     for (enemy in enemies)
     {
         var dist = Vector3.Distance(enemy.transform.position, player.transform.position);
         if (dist < 8)
         {
             enemy.Send$$anonymous$$essage("ApplyDamage", 100);
         }
     }
 }

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

14 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

Related Questions

How can I fix this so it sends the messages to the game objects with the tag? 2 Answers

Transform - GameObject? 1 Answer

GameObject follow Tagged Object 1 Answer

lookAt nearest object to another object 1 Answer

Destroy all GameObjects at once 5 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