Measure between 2 Objects & show in UI
Hi All,
I have been looking all day for an answer to this and have found loads that just don't seem to work, I want to get/ display the distance between 2 objects.
Eventually I would like to get the X/Y/Z values as well as the Vector3, that's why the other floats are in their.
Below is what I have got so far and it seems pretty close, I'm just getting a null reference on Measure1, when I try and start the game.
This is the closest I have got;
Thanks For any help upfront...
public class Measure : MonoBehaviour {
//public GUIElement distanceText;
public GameObject Measure1;
public GameObject Measure2;
public Text label;
public float Distance_;
//public float Distance_x;
//public float Distance_y;
//public float Distance_z;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
Distance_ = Vector3.Distance (Measure1.transform.position, Measure2.transform.position);
label.text = Distance_.ToString ("F2");
}
}
Answer by MT369MT · Aug 15, 2018 at 03:29 PM
Hi, your code should work well. The NullReferenceObject means that you forgot to assign your objects in the inspector (drag and drop your object from the hierarchy to the variable in the inspector). You could also assign them in script in Start() with:
Measure1 = GameObject.Find(“nameOfObject1”);
I did think that and the object is assigned, maybe I'll assign the objects in the script the way you said and see if that works, thank you!!
Answer by Tazza08 · Aug 15, 2018 at 08:41 PM
Still having the same issue, any other ideas?
Never $$anonymous$$d, I had the script on 2 different objects that must have been confusing it!!