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;
}
}
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.
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?
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 "}"