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
5
Question by kloowww · Apr 15, 2012 at 09:40 PM · gameobjectdistancefindclosest

GameObject.Find closest ??

How do i find the closest object or an object at a certain distance to that object ?

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 kloowww · Apr 15, 2012 at 11:19 PM 0
Share

how can i find the closest object with a certain name if there are multiple objects with the same name and that are instantiated when game starts and that doesn't have collider ?

Thanks for reply

2 Replies

· Add your reply
  • Sort: 
avatar image
10

Answer by Lttldude · Apr 15, 2012 at 11:31 PM

Give all the objects that you want to check if it's the closest a tag. You should only have to change the tag of the prefab that you are instantiating from

Then just substitute "tagname" with the tag you chose for those instantiated objects.

 function Update()
 {
     print("Closest Object is " + GetClosestObject("tagName").name);
 }
 
 function GetClosestObject(tag:String) : GameObject
 {
     var objectsWithTag = GameObject.FindGameObjectsWithTag(tag);
     var closestObject : GameObject;
     for (var obj : GameObject in objectsWithTag)
     {
         if(!closestObject)
         {
            closestObject = obj;
         }
         //compares distances
         if(Vector3.Distance(transform.position, obj.transform.position) <= Vector3.Distance(transform.position, closestObject.transform.position))
         {
            closestObject = obj;
         }
     }
     return closestObject;
 }
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 kloowww · Apr 16, 2012 at 12:00 AM 0
Share

Thanks this finds the closest object but i can't figure how to assign that object to a var like var object : Transform; // and here to be assigned that object

Thanks for reply

avatar image akauper · Jul 27, 2013 at 03:02 AM 0
Share

Thank you Lttldude for your response, it helped me greatly. However, I've now run into a problem with instantiating objects into the scene. After the code is running and an object has found the closest object tagged ("enemy"), if you instantiate a new object tagged ("enemy") into the scene, the current object loses its closest object and cant regain it. Any idea why?

avatar image jquievreux · Dec 14, 2015 at 09:57 AM 0
Share

just a little add: remove : if(!closestObject) { closestObject = obj; }

and use : var closestObject : GameObject= objectsWithTag[0]; I'm not sure of the syntax I use csharp.

avatar image
3

Answer by Lttldude · Apr 15, 2012 at 10:10 PM

Here is a function that should return the closest gameobject with a collider based on specific distance away from the current object's position.

 var radius : float = 50.0;  //this is how far it checks for other objects
 
 function Update()
 {
     print("Closest Object is " + GetClosestObject().name);
 }
 
 function GetClosestObject() : GameObject
 {
     var colliders : Collider[] = Physics.OverlapSphere (transform.position, radius);
     var closestCollider : Collider;
     for (var hit : Collider in colliders) {
         //checks if it's hitting itself
         if(hit.collider == transform.collider)
         {
             continue;
         }
         if(!closestCollider)
         {
             closestCollider = hit;
         }
         //compares distances
         if(Vector3.Distance(transform.position, hit.transform.position) <= Vector3.Distance(transform.position, closestCollider.transform.position))
         {
             closestCollider = hit;
         }
     }
     return closestCollider.gameObject;
 }
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

9 People are following this question.

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

Related Questions

Dynamic Laser Fence Scripting Problem 1 Answer

How do I find the closest target with a tag? c# 2 Answers

how to find the closest (and second closest) object with tag - one object 4 Answers

OnMouseOver Distance Measuring 1 Answer

I wanna use Gameobject.Find(string name, bool active) 3 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