- Home /
How to detect objects around a position and add them to a list
So Im making a simple game of stacking pins onto a spinning platform. All is working as it should be but im struggling to detect the colours around the pin once it hits the platform.
I want it so that once it hits the platform it will perform a check for the surrounding pins and if they are on the same layer then add them to a list. If 3 or more pins are detected in the list then draw a line between all the pins and pop them. I've tried a few methods but they are not working for me. Im thinking of casting a spherecast around the object but im not sure if this will work. Any help is great, thanks!
Answer by Dorinbuzilov · May 25, 2018 at 03:26 PM
So I have found a solution to my problem. Using a list and Physics2D.OverlapCircleAll , im able to detect the surrounding colours by comparing their color layer vs the current pin colour layer. Also, if the nearby pins have pins which are close-by to them but not to the current pin it will pass their connected pin onto the current one and still form a chain combo of 3+ pins.
My problem now is how do I draw a line renderer connection between the pins. I've tried creating a new gameobject with a line renderer and passing the pins positions to the line renderer but I t doesn't seem to work for me.
Answer by oussamahassini6 · May 25, 2018 at 11:20 AM
create an array to store all the instatiated gameObject and be sure to give them the right color in the sprite renderer and when you find pins with the same color just pick them from the array and destroy. hope it was helpfull please a good vote @Dorinbuzilov
Your answer
![](https://koobas.hobune.stream/wayback/20220612163410im_/https://answers.unity.com/themes/thub/images/avi.jpg)