How to get distance between player and object

I need to know how to get the distance between the player (first person controller) and a object such as a car (its for an objective) and I need it to auto update with every step

var distance : GUIText
var other : Transform;

if (other) {
    var dist = Vector3.Distance(other.position, transform.position);
    GUI.text (Rect (10, 10, 100, 20), ""Distance to other: " + dist");
}
  • GUI and GUIText are two very different things.
  • GUI functions must be executed inside OnGUI() {}.
  • Things you want executed every frame, can be put inside Update() {}.

#pragma strict

var other : Transform;
private var dist : float;
 
function Update() {
	if (other != null) {
	    dist = Vector3.Distance(other.position, transform.position);
	}
}

function OnGUI() {
	GUI.Label(Rect (10, 10, 200, 20), "Distance to other: " + dist);
}

This script has several errors: you’re messing GUIText and the GUI system. If using the GUI system, the code could be like this:

var other: Transform;

function OnGUI(){
  if (other){
    var dist = Vector3.Distance(other.position, transform.position);
    GUI.Label(Rect(10, 10, 100, 20), "Distance to other: " + dist);
  }
}