- Home /
is there a way to count how many gameobjects are adjacent to each other and have the same tag?
I know that you can use findgameobjectswithtag to determine all objects with a tag in an area, but the problem is that the area is not rigid as the objects are moved around during gameplay. I need a way to count the amount of these objects with a tag that are adjacent to each other/touching. The objects also have the same material and all have the same script attached if that helps. Any help would be much appreciated. Thanks.
Answer by jimmycrazyskills · Dec 18, 2018 at 01:44 PM
Hi mate, I don't know if this would work for you, but could you have have an array of all touching objects, then add to it each time within onCollisionEnter
when a specfic tag hits another object with the same tag. Inside the array you could store a unique identifier for each game object to ensure the same one isn't added twice. Then remove from the array onCollisonExit
.
NOTE: This just an idea and it would only count touching objects, I don't know if you wanted to check if they were in a line or actually touching.
Thanks for your reply, that advice got me most of the way however if a few objects with the same tag are on one side of the screen, and another few objects with that same tag are on the opposite end of the screen, it will still add them all to the array when they are not all adjacent to each other. Any ideas? Thanks.
Never$$anonymous$$d, I just made it that if the list is empty the objects are entered and then if the collision object is already in the list then add it to the list, so the object has to hit something that is already in the list. Then when i want to know the number of items in the list I put system.linq in as a namespace and then use .todistinct. Thank you for helping me!
Your answer
Follow this Question
Related Questions
physics.OverlapSphere colliders 1 Answer
Destroying objects with the same name that are touching eachother. 1 Answer
Script is only working when slowing down timescale 0 Answers
What are best practices for getting your character to not fall through the floor? 1 Answer
Strange Collisions in 2D 1 Answer