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 DayyanSisson · Apr 08, 2012 at 06:24 AM · foreachrangefindgameobjectswithtagarraylistphysics.overlapsphere

FindWithTag in certain range

I'm trying to add nodes to an Array but the nodes need to be within a certain range. I tried this to see if I could achieve this:

 Collider[] colls = Physics.OverlapSphere(AI.position, distance);
         foreach (Collider col in colls){
             nodes = GameObject.FindGameObjectsWithTag("Node");
         }

I knew it wouldn't work, so what I tried to do was individually add each node inside the foreach:

 Collider[] colls = Physics.OverlapSphere(AI.position, distance);
         foreach (Collider col in colls){
             nodes.Add(col);
         }

And I made 'nodes' an ArrayList. Unfortunately though, it kept adding the same nodes over and over again until I had hundreds of the same nodes inside my ArrayList. How should I FindGameObjectsWithTag inside a certain range?

Comment
Add comment · Show 2
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 GuyTidhar · Apr 08, 2012 at 08:06 AM 0
Share

I am wondering what comes before and after this code? Could it be that you are running this each frame? Cause if you do, you will add the same colliders each frame until the overlap stops...

avatar image DayyanSisson · Apr 08, 2012 at 05:09 PM 0
Share

Yeah it's in update because it needs to be dynamically searching for new nodes every frame.

2 Replies

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

Answer by save · Apr 08, 2012 at 09:03 AM

Simply change the tag after it's made it into the list. You should consider using a collider set to trigger instead if this has to run every frame.

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 DayyanSisson · Apr 08, 2012 at 05:10 PM 0
Share

Well the other problem is that it needs to be within a certain range. Your answer should work to keep it from re-adding itself, but would it still keep the nodes within a certain range?

avatar image DayyanSisson · Apr 08, 2012 at 05:53 PM 0
Share

Wait a second...this does solve the problem! Thanks. And to find the nodes in the range, the script ended up like this:

 Collider[] colls = Physics.OverlapSphere(AI.position, distance);
         foreach (Collider col in colls){
             if(col.tag == "Node"){
                 nodes.Add(col);
                 col.tag = "NodeU";
             }
         }

Thanks a lot! Not sure why I didn't think of this before....

avatar image DayyanSisson · Apr 08, 2012 at 06:23 PM 0
Share

One of the things I need though that I forgot to ask is if the nodes are no longer inside the OverlapSphere, that they are removed from the ArrayList 'nodes' and that their tag is no longer 'NodeU'. You would this it'd be something simple like:

 foreach (Collider col !in colls){

But it doesn't work that way. Is there a way to do this?

avatar image DayyanSisson · Apr 08, 2012 at 11:12 PM 0
Share

Oh never$$anonymous$$d, figured that out too. Thanks for your help.

avatar image
0

Answer by darkcookie · Apr 08, 2012 at 09:07 AM

http://unity3d.com/support/documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html

theres an example code there ...hope it helps

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 DayyanSisson · Apr 08, 2012 at 05:36 PM 0
Share

I have no idea why this happens, but the code gives up a 'use of unassigned local variable 'closest'' error when I copy and paste it....so Unity should get that checked out, because the error is in the middle of the script, so I have no idea why it's giving up an error.

avatar image DayyanSisson · Apr 08, 2012 at 05:43 PM 0
Share

And that also solves my problem for the nearest node, but it doesn't solve my problem for nodes within a range, and I'm not sure how to change it to do that. Thanks though. Oh, and I fixed the error.

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

7 People are following this question.

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

Related Questions

foreach fills array with 1 Answer

Why is FindGameObjectsWithTag ignoring my Physics.OverlapSphere? 1 Answer

arraylist doesn't stop adding to string-infinite c# 1 Answer

GameObject.FindGameObjectsWithTag problems 2 Answers

Using a Parameterized arraylist (C#)??? 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