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 black843 · May 02, 2015 at 03:33 PM · loopforeachlogic

foreach-loop. Is there a gameobject with position existing?

Hey!

I'm working around with arrays and I would like to know how to look for a gameobject by its Position.

Example Scriptpart:

 public GameObject[] weels;
 
 void Update()
 {
   foreach(GameObject weel in weels)
 {
   //Check if there a weel existing with Position Vector(x,y,z)
 }
 }

Please help!

thanks in advance!

Comment
Add comment · Show 3
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 Magnomous · May 02, 2015 at 03:37 PM 0
Share

maybe:

 if(weel.transform.position == new Vector3(x, y, z))
     // do stuff
avatar image black843 · May 02, 2015 at 03:42 PM 0
Share

I need something like that

 if(weel.position[x,y,z] == null)
 {
 //DO SO$$anonymous$$ETHING
 }
 else
 {
 //DO NOTHING
 }

avatar image Magnomous · May 02, 2015 at 03:56 PM 0
Share

Would raycasting solve your problem?

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by glennhk · May 02, 2015 at 03:37 PM

Check the distance between every wheel in the array and the target position using Vector3.Distance, or by computing the magnitude (sqrMagnitude is better for efficiency), and check if the distance is within a fixed threshold.

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 black843 · May 02, 2015 at 03:43 PM 0
Share

sorry, but that doesen't help me. pls look to upper comment. tks

avatar image
0

Answer by Bunny83 · May 02, 2015 at 03:42 PM

You can't directly access a gameobject by it's position. You also shouldn't compare the position of gameobjects with a given coordinate as due to floating point accuracy issues this won't work. What you usually do is specifying a limit / range to look for a gameobject.

 public GameObject[] weels;
 public Vector3 somePosition;
  
 void Update()
 {
     foreach(GameObject weel in weels)
     {
         if ((weel.transform.position - somePosition).magnitude < 1f)
             Debug.Log("The object " + weel.name + " is closer than 1 unit to " + somePosition);
     }
 }

You can also use the Distance method which does the same thing:

         if (Vector3.Distance(weel.transform.position, somePosition) < 1f)
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 black843 · May 02, 2015 at 03:47 PM 0
Share

Yeah, I know what u want to do, but in my game, I need to check first if there is a gameobject existing in the position....

avatar image glennhk · May 02, 2015 at 04:55 PM 0
Share

That approach is clearly error-prone. There can be 0, 1, 2, etc. GameObjects in a single point. If you need to query the nearest GameObjects in a position, put a collider on each wheel, configure a layer for the wheels in order to disable unwanted collisions and use an OverlapSphere on that particular layer's mask.

avatar image
0

Answer by Addyarb · May 02, 2015 at 05:14 PM

As you can see from the other responses, using Vector3 positions sets you up to run into all sorts of problems, not to mention it's inefficient.

If you don't mind, I'll suggest an (imo) easier/better way, and you can comment if this wouldn't work in your particular situation. I think the best way to achieve this is to completely scrap the idea of locating an object via Vector3 and use Tags instead.

So my solution would look something like this:

  1. Create a new tag called "Wheel."

  2. Tag each of your wheels as "Wheel."

  3. Use this script to get them into an array and access them if they're extant.

  4. Be sure and attach this script to the root gameObject (your car?) or alter the script a bit in order to check the wheels.

      GameObject[] wheels; //The wheels on my car
             
             void Start()
             {
             //find all the wheels on my car by searching their tag.
             wheels = GameObject.FindGameObjectsWithTag("Wheel");
             }
             
             void Update()
             {
             foreach(GameObject wheel in wheels){
             //If the wheel exists, and it is attached to this car..
             if(wheel.activeInHierarchy && wheel.transform.root == transform){
             //this code will only happen if wheel is enabled
             }
             }
             }
     
    
    
    
    
    
    
    
    
    
    
    
    
    
    
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

23 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

Related Questions

Logic question, unique path finding system 2 Answers

How can I do a foreach loop for an array of booleans? 1 Answer

Guarantee loop order of child objects. 1 Answer

foreach Gameobject in array problems 1 Answer

Foreach with GameObject.Find() 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