GUI text displaying an object position ?

Hi people,
so sorry to open a thread for question that might seem so simple/stupid for many coders.
But I did not found the solution in past subjects and seem not able to find the damn script myself…

So basically I need a gui text that displays the Y position of the camera while it walks through the scene (project is not a game but a urban-landscape scene with much relief and user may need to always know the altitude he or she is).

To be exact the position that needs to be displayed is not the camera’s one but its parent (parent moves). Anyway it’s same solution for script I guess…

I tried to attach following script to my text but of course it does not work.

Not only it does not work but I get an error window saying that “MyCamera” is an unknown identifier.
Same if I replace in the script the name of the camera by the name of any GameObject of the scene…

Seems there is a relation between this script and the rest of the scene, that I don’t get…

PS : subsidiary question. I only use free version of Unity for the moment and would like to see how render the real time cast shadows (mean I wonder if I can “afford” them while I work on quite large scenes with many trees and plants…).
Any people have links to games or other unity projectors demonstrating about it ? Did not see any on the Unity demo page, maybe I missed some…?

you can’t access game objects directly by their name like that, the program tries to find a variable MyCamera, which not defined.

you can find game objects by using the Find() method

that way you can probably Find() the parent of the camera directly

in your case you could probably also find your camera using Camera.main

for some information about finding the parent object see this link

function Update () {
    var camPosY:float = Camera.main.transform.position.y;
    guiText.text = camPosY.ToString();
}

Be sure to read the above documents too if you wish to improve yourself further.

wow…
life changes when people help you to use your brain.

Thanx to you dpb. When you wrote you can’t access objects directly by their name it made me think that… curious… I ALREADY use another script directly accessing objects name.
It’s actually a script attached to a GUI texture (a button) that allows to switch from a camera to the other (built this script from parts of posts I read here, of course).

Thanx you made me remind this, I looked how this first script does this part of the job, no need “Find()” or whatever.

Mortiis gave the end of the script, thanx to him.

The solution is :

var obj1 : GameObject;

function Update () {
    var camPosY:float = obj1.transform.position.y;
    guiText.text = camPosY.ToString() + " m";
}

Then I just have to select who is “obj1” > my camera parent in the pop menu…
Works perfectly.

Only thing is I should find and add some math formula in the script so that it displays only 2 decimals.
“37,32 m” would be enough.
No need “37,31937 m” :lol:

You can just show it as “37” if you declare the variable not as float but as int, like;

var camPosY:int = Camera.main.transform.position.y;

SOLVED !!!
:smile: :smile: :smile:

var obj1 : GameObject; 

function Update () { 
// I get the Y value of my object
var camPosY:float = obj1.transform.position.y; 
// multiply it by 100
var foiscent = (camPosY * 100);
// convert this value in integer 
var entier:int = foiscent;
// then re-convert this integer in float number
// then divide by 100
var resulta:float = entier;
var resultb = (resulta / 100);

guiText.text = resultb.ToString() + " m"; 
}

Basically :
If my object is at 8,43748 m

I transform it in 843,748
Then integer = 843 m only
Then “allow” the float
Then redivide by 100 => 8,43 m