• 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 osamareda945 · Feb 29, 2020 at 10:14 AM · gameobjectexceptionfindobjectoftype

how to Find Objects Of Type<> but with But with an exception

Hi, I am working on a script that is supposed to find the closest object to a group of objects in the game ... Of course with determining the type of these objects using another script ... In any case it works well but it chooses the body that has the script as the closest object ... which is Something logical ... but I want to choose any of the available objects except the one that has the script

Important note: I also want to make the object carrying the script the same type of object that must be chosen the closest one in it

 public void findTheNearestObject()
     {
         float distanceToTheNearestobject = Mathf.Infinity;
         objects1 nearestObject = null;
         objects1[] allObjects = GameObject.FindObjectsOfType<objects1>();
 
         foreach(objects1 currentObjects in allObjects)
         {
             float distenceToObjects = (currentObjects.transform.position - this.transform.position).sqrMagnitude;
             if (distenceToObjects < distanceToTheNearestobject)
             {
                 distanceToTheNearestobject = distenceToObjects;
                 nearestObject = currentObjects;
 
                 this.transform.position = (this.transform.position - nearestObject.transform.position).normalized * dist + nearestObject.transform.position;
             }
         }
       
         Debug.Log("the nearst object is :" + nearestObject);
         Debug.DrawLine(this.transform.position, nearestObject.transform.position);
     }

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
Best Answer

Answer by Bunny83 · Feb 29, 2020 at 11:58 AM

Well that's pretty trivial. You just have to know the script instance on your own object. We don't know where you attached your "objects1" instances (btw: terrible name. Please look up guidelines for class names in C#). I assume that the script is just attached to the same gameobject this script is attached to. In that case you can simply do:

 objects1[] allObjects = GameObject.FindObjectsOfType<objects1>();
 objects1 myOwnObject = GetComponent<objects1>();

inside your loop you can simply add this right at the beginning inside the loop:

 if (currentObjects == myOwnObject)
     continue;

This will skip the current object if it's our own. If the script is not on the same object this script is attached to you have to use either GetComponentInChildren (if it's on a nested object) or GetComponentInParent (if it's on a parent object).


ps: You really need to work on your naming ^^. Don't use the plural for variables which only holds a single object. Class names should always start with a capital letter and be descriptive what it represents. Since your component is attached to a gameobject and is only relevant for that one object it's attached to using the plural makes no sense. You only use the plural in classnames if the class itself represents a collection. Even in this case it's more common to use the suffix "Collection".


Use meaningful names. "Object" is the most generic name possible. Choose a name that descibes what this class actually does or represents.


Finally the last line in your if statement inside your loop doesn't make much sense. You actually move the object you try to find the closest object to around while iterating through the objects. So the result would make not much sense. That's also another bad practise. You named your method "findTheNearestObject" but it doesn't actually return anything and has unexpected sideeffects which the name does not suggest.

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

Answer by kskjadav007 · Feb 29, 2020 at 11:38 AM

     public void findTheNearestObject()
     {
 
 
 
 
         float distanceToTheNearestobject = Mathf.Infinity;
         objects1 nearestObject = null;
         objects1[] allObjects = GameObject.FindObjectsOfType<objects1>();
 
 
         if (allObjects.Length>0)
         {
 
             foreach (objects1 currentObjects in allObjects)
             {
                 float distenceToObjects = (currentObjects.transform.position - this.transform.position).sqrMagnitude;
                 if (distenceToObjects < distanceToTheNearestobject)
                 {
                     distanceToTheNearestobject = distenceToObjects;
                     nearestObject = currentObjects;
 
                     this.transform.position = (this.transform.position - nearestObject.transform.position).normalized * dist + nearestObject.transform.position;
                 }
             }
 
             Debug.Log("the nearst object is :" + nearestObject);
             Debug.DrawLine(this.transform.position, nearestObject.transform.position);
         }
         else
         {
             Debug.Log("No object found");
         }
 
     }


Something like this ?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

191 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 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 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 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 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 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 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

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

NullReferenceException in follow function 0 Answers

Accessing other GameObjects Script Variables 2 Answers

How can I do that in an array with gameobject.findobjectoftype but that does not count the same object in which this script is? 1 Answer

GameObject has been destroyed, but you are still trying to access it. 0 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