Showing Text as soon as player is near enough


I have different objects in my level and I want them to be collectable. As soon as the player is nearby an object, a text message appears and he can take it ("Press E to take object").

I attached the same script to some of my objects, but 2 problems appear:

  • The text message flickers, as soon as i get near an object. The visibility value jumps from true to false.
  • The text message only appears for one of the objects but not for the rest. As soon as I take one object, it works for the next one and so on. But never for all of them at the same time.

Thx for your help!

var gameCharacter : Transform; 
var distance : float; 
var isTakeable = true;

function Start() { 
   gameCharacter = GameObject.Find("First Person Controller").GetComponent(Transform); 

function Update () { 

   distance = Mathf.Sqrt((gameCharacter.position - transform.position).sqrMagnitude); 

   if (distance < 6.0) {
   // Showing some text as soon as we are near enough
   GUI_Main.showTakeNotice = true;

         // if we press E the object and the text disappear
     if(Input.GetKey("e") && isTakeable == true){ 
         GUI_Main.showTakeNotice = false;

   if (distance >= 6.0) {
   GUI_Main.showTakeNotice = false;

If you have multiple of these Scripts attached to objects, they all set the GUI_Main.showTakeNotice variable. So if one object is close enough it sets it on true, but if another object isnt close enough it sets the GUI_Main.showTakeNotice on false.

So that's why its flickering.

And for the distance calculation, you can use Vector3.Distance too ;)

Thx for the Vector3.Distance hint. I tried it out and assigned the First Person Controller as argument. It looks like this now:

var dist = Vector3.Distance(other.position, transform.position);

    if (dist < 6.0) { ...

But it is still measuring the distance to the first object, I attached the script to and not for the rest. So the text still appears in front of only one object.

And concerning the flickering: There must be another reason for that. I tried the script for only one object and it is still flickering...

Any ideas?