Activate Object in a Certain Distance

I want to change a Boolean on multiple or a single object when its within a certain distance. Im new to c# and I’d much rather program this game in c#. So i was thinking of having some variables like these.

 public int Radius = 1;
 public GameObject ActivatedObjects = GameObject.FindWithTag("activate");

Could I set up a function to where it finds game objects with the activate tag (activated objects variable) within a certain distance (radius variable) and than have a Boolean in those objects that were found and change it from false to true or the other way around. Much help is appreciated.

Thanks

2 Answers

2

I often use trigger areas that activates objects, this way you won’t have to worry about clutching up the overhead to much. While the objects are active I check for a distance > player trigger area (using sqrMagnitude), if it is then disable their demanding components.

This require rigidbodies though, if your game doesn’t use physics then run a invoked coroutine checking for sqrMagnitude on the objects.

Thanks! Not even my question but the idea of trigger areas will help me soon. =)

See my answer to your duplicate question- http://answers.unity3d.com/questions/168486/changing-booleans-throught-certain-distance.html. You should avoid posting duplicates- remember that when you have below 15 reputation, it takes a few minutes for your questions to be cleared by the moderators! You just have to be patient.

In the meantime, please close and/or delete any duplicate questions.