Displaying Distance on GUI

Hey Everyone!!

The following is my code for reporting the distance between my character and the object which in this case if plant 5

#pragma strict
var targetObject : Transform;

//this line is going to find the target object assigned to the script, in this case it is “plant5”
function Start () {

targetObject = gameObject.Find(“plant5”).transform;

}

//this line is stating how far away the user is from the target object
function Update () {

if (targetObject ) {
var dist : float = Vector3.Distance(targetObject.position, this.transform.position);
guiText.text= "Distance to plant 5 " + transform.position.x;
}

}

I have one porlbme with this code though, the text isn’t showing up on my GUI even though I already hacve assigned the GUIText thing to the character from the component menu drop down thing. Is there anyway to get the distance to show on my GUI ?

Thank you

It can mean 2 things:

  1. targetObject is null (not found by the ‘gameObject.Find(“plant5”).transform’ line)
  2. there is a problem with GUIText

To test it for 1) put the line ‘Debug.Log(targetObject)’ right after the ‘gameObject.Find…’ line
To test it for 2) drop out the whole ‘if (targetObject )’ thing and have ‘guiText.text=“Foo”’

… but since the distance calculation didn’t break, it’s probably the second thing. So just try to get the dummy output first.

You got to learn how to locate a real problem, before posting to the forum, because it increases the odds of getting an answer. :slight_smile: