- Home /
Changing Booleans Throught Certain Distance
I am trying to change a boolean on a single or multiple objects through a certain radius.
I was thinking of having these variables set up
public int Radius = 1;
public GameObject Activate = GameObject.FindWithTag("activated");
I was thinking that you guys at unityanswers could help me make a script that would find GameObjects with the tag activated and if there in a certain distance the script placed on those objects would have a boolean and it would make it true.
Comment
Answer by syclamoth · Sep 20, 2011 at 02:03 AM
how about this-
public double radius = 1;
private Transform myTrans;
Start()
{
// This makes it so that you don't need to do as many inefficent
// dynamic searches, assuming you want to do this every frame
myTrans = transform;
}
// Then, wherever you need to do the check-
GameObject[] foundObjects = GameObject.FindGameObjectsWithTag("activated");
foreach(GameObject obj in foundObjects)
{
ScriptWithBooleanOnIt objScript = obj.GetComponent<ScriptWithBooleanOnIt>();
if(objScript == null)
{
// This makes sure that your object in question contains the script
continue;
}
// This will also set the value to false if the object goes back out of range.
objScript.booleanValue = Vector3.Distance(obj.transform.position, myTrans.position) < radius;
}
And then put a MonoBehaviour on every object you want to affect with this called 'ScriptWithBooleanOnIt' (name not important), and make sure it has a public bool called 'booleanValue'.
Your answer
![](https://koobas.hobune.stream/wayback/20220612220221im_/https://answers.unity.com/themes/thub/images/avi.jpg)