Show GUI text when Distance < 6

Hi!

I have a simple script that shows some text as soon as I am near to an object. This works so far but only for 1 object at a time. As soon as I attach the script to another game object, it only works for the first one. Why is that and how can I solve this problem? Thx for the help.

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

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

function Update () {  

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

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

   else {
   GUI_Main.showNotice = false;
   } 
} 

This probably is because the other scripts will switch the GUI off: GUI_Main.showNotice = false;

If you attach this to multiple GO's it'll only work if all the GOS are close to the object.

You are right, that is the problem. Any ideas how to change the script to work for multiple GO's that are not close to another?

Do you want to show the text for all objects in your scene or for some kind of objects? If it is the latter, you can try to tag those kind of objects and attaching your script to player. Then in your script you check for the tag an depending on the result you show or hide the text.

So, I think it would be useful to show you the Unity example. You see some kind of a picture gallery.

http://www.oinobareion.at/files/scripting_problem.html

What I want: As soon as the player comes close to the painting in front of him, the text "Press E to choose painting" appears. I want this to work for all the paintings in the gallery, but by now, it only works for one. It only takes the distance for one picture and not for the rest although the script is attached to every painting in this room. How can I do that?

I'm totally left out in the dark here :-)

What i would do, is give al the paintings the tag "Painting". Then make a general script to add to your camera with the following code: (C#)

public class Test : MonoBehaviour

{

private Transform Player;

void Start()

{

    Player = GameObject.Find("First Person Controller").transform;
}

void OnGUI()
{
    float yourMaxDistance = 6;
    bool objectCloseEnough = false;
    foreach (GameObject obj in GameObject.FindGameObjectsWithTag("YourPaintingTag"))
    {
        if (Vector3.Distance(obj.transform.position, Player.position) <= yourMaxDistance)
        {
            objectCloseEnough = true;
            break;
        }
    }
    if (objectCloseEnough)
    {
        GUI_Main.showNotice = true;
    }
    else
    {
        GUI_Main.showNotice = false;
    }
}

}

What this does, is lookup all the gameobjects with the tag "YourPaintingTag". Then checks if one object is near enough to pickup. If so, it sets the notice.

//I dont know why the code is displayed strange. Just copy everything from "public class" to the last "}"

Since multiple GameObjects will share this script, surely the only one to take effect is the last one being updated.

I would recommend you doing instead:

Have an empty game object with a script which will have a update function running this code every once in a while:

GUI_Main.showNotice = false;

Then, in the same script, have another function called Enable(), which will do this:

GUI_Main.showNotice = true;

Now replace the Update() function of your object with

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

   if (dist < 6.0) 
   {    
      theEmptyGameObject.GetComponent("MyAwesomeScript").Enable()
   }

Now the one in charge of "disabling" the GUI thing will be only one object (the empty game object you created) instead of multiple ones.

Thank you. But the script didn't compile. Unfortunately I am not familiar with C#

Player = GameObject.Find("First Person Controller").GetComponent();

Expression denotes a 'type', where a `variable', 'value' or 'method group' was expected.

Do you know, where the problem could be?