Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Inryhk · Aug 15, 2019 at 08:52 AM · detectionspawning problemslayermaskunique-identifiers

Detecting multiple unique gameobjects using LayerMasks ?

I have a random generator which generates a dungeon using it's own rooms as spawners (each one spawn correct connections to itself), and or rare occasions I have a bug where two rooms are spawned on top of each other because they try to detect room, return with nothing, and spawn at the same time. I also have this "sensors" placed at each potential room location that I use as wrap up and fill for my level generation.
Now, I want my sensors to be able to detect if there are two or more rooms at their location so that they can destroy them and put a 4 way room instead. I don't want all my rooms to have a Rigidbody2d so I've been using Layermasks Collider2d x = Physics2d.OverlapCircle instead of collisions Collider2d.OnCollisionEnter to detect rooms so far.

 public LayerMask room;
 public List<GameObject> rooms;
 
 public void Update()
     {
 Collider2D roomDetection = Physics2D.OverlapCircle(transform.position, 1, room);
 roomDetection.GetComponent<RoomManager>().Count(); //adds Gameobject to rooms list only once. 
 if (rooms.Count >= 2)
             {
                 Debug.Log("Destroyed One");
                 GameObject.Destroy(rooms[0]);
             }
 }

This one only detected one room that was on top of the other so it never destroyed anything, I've also tried using array and OverlapCircleAll but couldn't get it to only detect unique gameobjects.

At this point, I'm sure I've been going about it in a very roundabout way so If you know of an easy way to detect if there are multiple unique objects of the same type/tag/layer at the current object transform, accessing them and destroying them without the need for Rigidbody2d, I would appreciate suggestions.

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 Inryhk · Aug 15, 2019 at 10:41 AM

For now inside my room scripts(objects that I wanted to destroy if there were multiple) I've put a line:

 public void MultipleRooms()
     {
         Collider2D other = Physics2D.OverlapCircle(transform.position, 1, room);
         string otherTag = other.gameObject.tag;
         if (other.gameObject != gameObject && gameObject.tag == otherTag)
         {
             Destroy();
         }
     }

And I invoke it few seconds after a room have been instantiated. For whatever reason it seems to work. Really thought both rooms would destroy at the same time with this solution.
Still even it this achieves the effect that I want, I would prefer if those seperate sensor gameobjects govern that part of generation and not the rooms themselves.

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

178 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

Related Questions

Collision Detection Help! 1 Answer

How to random place sprites but make sure they aren't touching? 1 Answer

2D Object Pickup - Occasionally adding score twice 0 Answers

How can I stop the Camera from detecting things behind walls. 0 Answers

Can't change an object Layers at runtime via c# code (Help Please) 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