Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 glatteinfo · Sep 29, 2021 at 07:13 PM · spherefindgameobjectswithtag

Physics.OverlapSphere - do an action only if there are no other gameobjects with tagX around

Hello there!

I try to cast a sphere around a hit.point and identify, whether there are already other objects within that sphere with a specific tag (XMark). If not, an object should be spawned. Does Physics.OverlapSphere maybe not work if instantiated objects? Or am i getting it wrong? From what i find on the internet this should work :-/ Maybe anyone of you can give me a hint how i can do it right?

Thanks a million!

Here is the Code:

   MarkXPosition = hit.point;
    
                 // Physics.OverlapSphere(center, radius);
                 Collider[] MarkXFindSphere = Physics.OverlapSphere(MarkXPosition, 5f);
                 Debug.Log("Checking if XMarks exist closeby ");
 
                 foreach (Collider Test in MarkXFindSphere)
                 {
                     if (Test.gameObject.tag != ("XMark"))
                     {
                         Debug.Log("No XMarks found nearby, spawning one");
                         Instantiate(XMark, MarkXPosition, XMark.transform.rotation);
                     }
 
                     else
                     {
                         Debug.Log("An XMark already exists nearby! Not spawning XMark");
                     }
 
                 }
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
0

Answer by glatteinfo · Sep 29, 2021 at 08:40 PM

I guess I found a solution: just in case someone has a simular problem I'm posting it here. The code seems to check for all objects without the tag and spawns one Object for each of them. So I helped myself with adding a counter whenever one object was found with the tag the instantiate wouldnt run since the counter isn't 0 anymore.

 MarkXPosition = hit.point;
 
                 // Physics.OverlapSphere(center, radius);
                 Collider[] MarkXFindSphere = Physics.OverlapSphere(MarkXPosition, 2f);
                 Debug.Log("Checking if XMarks exist closeby ");
 
                 foreach (Collider Test in MarkXFindSphere)
                 {
                    
                     if (Test.gameObject.tag == ("XMark" ))
                     {
                         XmarkAmount = XmarkAmount +1;
                         return;
                     }
                     else
                     {
                         XmarkAmount = 0;
                     }
   
                 }
 
                 if (XmarkAmount == 0)
                 {
                     Debug.Log("No XMarks found nearby, spawning one");
                     Instantiate(XMark, MarkXPosition, XMark.transform.rotation);
                 }
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 bdubbert · Sep 29, 2021 at 09:15 PM 0
Share

It seems like the "else XmarkAmount = 0" part shouldn't be there, otherwise if the last object returned isn't an Xmark it will always spawn an xmark

avatar image glatteinfo bdubbert · Sep 30, 2021 at 05:02 AM 0
Share

your absolutely right, thanks!!

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

126 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

Related Questions

Sphere with collider entering the flat surface 2 Answers

Set image or texture on only particular part of a sphere 2 Answers

Physics.OverlapSphere 2 Answers

[C#] how to add a primitive in this script??? 3 Answers

Rotate RigidBody Sphere 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