Add script for collision-checking


A number of people walk around in my game. People leave the world and get destroyed and people get created in the world.

At some point (for example every 15 sec) I find a gameobject by using gameObject.FindObjectWithTag(“People”). For only that gameobject I want to check if it has a collision with a building (buildings are tagged with the name “building”).

I could use the OnCollison methods in the script which is attached to all people.
But what I don’t want is that every object of people is checking every frame if it has a collison with a building (OnCollision-method).

My way to solve this would be a “CollisionCheckingScript” which I add to the specific gameObject I found with gameObject.FindObjectWithTag. But I don’t know if this is the best way to solve my problem.

The CollisionCheckingScript would only contain a OnCollison method.

Well it seems like you will be checking Oncollision no matter what happens.

A way to do it though is to set a variable, that you can see in the inspector, that gets a +1 counter every time it hits a building.

Or if there are substantially more people then buildings you can put that script onto the building.