If you are going to have lots and lots of these, I recommend you use a single manager class which controls all of them at once, and to help with this, use the CullingGroup API.
Something like this (untested):
This script would go on each text object:
class RegisterInCullingGroup : MonoBehaviour {
void Start() {
CullingGroupManager.Instance.AddObject(this);
}
void OnDestroy() {
CullingGroupManager.Instance.RemoveObject(this);
}
}
And another GameObject in the scene will have this component:
class CullingGroupManager : MonoBehaviour {
public static CullingGroupManager Instance;
public float radius = 1;
private CullingGroup group;
private BoundingSphere[] spheres;
private RegisterInCullingGroup[] objects;
private int count;
void Awake() {
Instance = this;
group = new CullingGroup();
group.targetCamera = Camera.main;
objects = FindObjectsOfType<RegisterInCullingGroup>();
spheres = new BoundingSphere[objects.length];
count = objects.length;
for (int i = 0; i < count; i++) {
spheres[i] = new BoundingSphere(objects[i].transform.position, radius);
}
group.SetBoundingSpheres(spheres);
group.SetBoundingSphereCount(count);
group.SetBoundingDistances(new float[]{100});
group.onStateChanged = OnStateChanged;
}
//to change the behavior when they disappear or appear, change this method
private void OnStateChanged(CullingGroupEvent sphere) {
if (sphere.hasBecomeVisible)
objects[index].gameObject.SetActive(true);
else if (sphere.hasBecomeInvisible)
objects[index].gameObject.SetActive(false);
}
void OnDestroy() {
group.Dispose();
group = null;
}
public void RemoveObject(RegisterInCullingGroup obj) {
int index = Array.IndexOf(objects, obj);
if (index == -1)
return;
group.EraseSwapBack(index);
CullingGroup.EraseSwapBack(index, objects, ref count);
}
public void AddObject(RegisterInCullingGroup obj) {
int index = Array.IndexIf(objects, obj);
if (index != -1)
return;
if (count == objects.length) {
Array.Resize(ref objects, count * 3 / 2 + 1);
Array.Resize(ref spheres, objects.length);
}
objects[count] = obj;
spheres[count] = BoundingSphere(obj.transform.position, radius);
++count;
group.SetBoundingSphereCount(count);
}
}
Yes, it does look significantly more complicated, but it is also going to be significantly more efficient. You can also use this sort of system for supporting multiple levels of detail for your stars etc by passing in more than one value when calling SetBoundingDistances, and checking which band the object is in inside of OnStateChanged (with the sphere.currentDistance property).