Disabling GameObjects With A Tag That Are More Than A Set Distance From A GameObject?

I’m not really sure how to approach this, any help would be appreciated. Unity Script is preferable. I only want gameobjects with a certain tag to be enabled when close enough to the player and actively switch between enabled and disabled depending on that distance.

Thanks

A few more ways to check distance. First, I like to make individual game object responsible for disabling themselves. That way I can have different behaviors for different objects. So some object (with the same tag) might be able to “see” further than another. This first section of code doesn’t make the game object inactive. It hides it and then uses the myActive to turn on or off other code:

#pragma strict

var dist = 1.5;
private var goPlayer : GameObject;
private var myActive = true;

function Start() {
     goPlayer = GameObject.Find("Player");
} 

function Update() {
	myActive = (Vector3.Distance(transform.position, goPlayer.transform.position) <= dist);
	renderer.enabled = myActive;
	if (!myActive) return;
	
	// Do the active object stuff

}

function LateUpdate() {
	if (!myActive) return;
}

Another way to do this is to use the fact that InvokeRepeating() continues to function even when an object is inactive. In addition when using InvokeRepeating() we can tune how often the check is made. With a value of 0.1, it is only made 10 times a second instead of the 30 or 60 times a second as happens if we use Update().

#pragma strict

var dist = 1.5;
private var myActive : boolean;

function Start () {
	myActive = gameObject.active;
	InvokeRepeating("CheckDistance", 0.0, 0.1);
}

function CheckDistance () {
	var act = (Vector3.Distance(transform.position, goPlayer.transform.position) <= dist);
	if (act != myActive) {
		gameObject.SetActive(act);
		myActive = act;
	}
}

If you want to check from “outside” (instead of having individual game objects make the check), and if all the individual tagged object are created once at the beginning of the game and never destroyed, you can use the following script. Note you may have to setup the script execution order to make sure this script gets executed after your tagged objects are created:

#pragma strict
var dist = 2.0;
private var goPlayer : GameObject;
private var argo : GameObject[];

function Start() {
     goPlayer = GameObject.Find("Player");
     argo = GameObject.FindGameObjectsWithTag("TagToFind");
} 

function Update() {
	for (var i = 0; i < argo.Length; i++) {
		var myActive = (Vector3.Distance(argo*.transform.position, goPlayer.transform.position) <= dist);*

_ argo*.SetActive(myActive);_
_
}_
_
}_
This next one disables from the outside and handles changing number of game objects. Note FindGameObjectsWithTags() does not find disabled game objects, so this code disables a specific script on the found game object (and could be extended to multiple scripts).
_
#pragma strict*_
var dist = 2.0;
private var goPlayer : GameObject;

function Start() {
goPlayer = GameObject.Find(“Player”);
}

function Update() {
* var argo = GameObject.FindGameObjectsWithTag(“TagToFind”);*
* for (var i = 0; i < argo.Length; i++) {*
_ var myActive = (Vector3.Distance(argo*.transform.position, goPlayer.transform.position) <= dist);
argo.renderer.enabled = myActive;
var comp : Behaviour = argo.GetComponent(Bug19);
if (comp != null) comp.enabled = myActive;
}
}*_

If the tagged objects have rigidbody components you could use built-in collision detection. In that case you could place a Sphere object around your player and disable the mesh renderer. Make sure its SphereCollider IsTrigger is checked.

Then just add this script to that sphere:

public class TriggerHide : MonoBehaviour {
	void OnTriggerEnter(Collider other){
		if(other.tag=="MyTag")other.gameObject.renderer.enabled = false;
	}
	void OnTriggerExit(Collider other){
		if(other.tag=="MyTag")other.gameObject.renderer.enabled = true;
	}
}

Also make sure your tagged object’s Collider also has IsTrigger checked or the object will collide with the sphere.

Alternatively you could just iterate through the tagged objects and test distance every update.

	void Update () {
		float collisionRadius = 1.0f;
		float d = Vector3.Distance(transform.position,taggedObject.transform.position);
		
		if(d < collisionRadius)taggedObject.renderer.enabled = false;
		else taggedObject.renderer.enabled = true;
	}