- Home /
Help with GUI.Box Problem?
I am creating a FPS-TPS Framework/Starter Kit and decided to come to you after my search in the docs failed. My compass script is displaying odd. It has two values like two boxes are sharing the same Rect. Here is an image as well as the script. Thanks for the help!
And the script. Its fairly basic so I assume it has something to do with the way Unity works internally. And yes, it is a modified version found on the UnityWiki. using UnityEngine; using System.Collections;
public class Compass : MonoBehaviour {
public float updateFreq = 1.0f;
private float _heading = 0.0f;
private string _headingString;
private Transform _gpsRef;
private float _gpsRefN;
private float timer = 0.0f;
public bool debug = false;
// Use this for initialization
void Start () {
Init();
}
// Update is called once per frame
void Update () {
FindHeading();
}
void Init() {
GameObject gpsRefGameObject = GameObject.FindGameObjectWithTag("GPSRef");
if(debug)Debug.Log("GPSReference: " + gpsRefGameObject);
if(gpsRefGameObject = null)
Debug.LogError("There is no GameObject named GPS Reference");
else if(gpsRefGameObject != null)
if(debug)Debug.Log("There is a GPS Reference GameObject");
else
if(debug)Debug.Log("There is something stored as GPS Reference");
if(gpsRefGameObject != null) {
_gpsRef = gpsRefGameObject.transform;
_gpsRefN = _gpsRef.rotation.eulerAngles.y;
}
}
void OnGUI() {
DisplayCompass();
}
public void FindHeading() {
timer += Time.deltaTime;
_heading = transform.rotation.eulerAngles.y - _gpsRefN;
if(_heading < 0)_heading += 360;
if(timer > (1 / updateFreq)) {
timer = 0;
}
_heading = Mathf.Round(_heading * 1f) / 1f;
_headingString = _heading.ToString() + " deg";
if(debug)Debug.Log(transform.rotation.eulerAngles.y + " - " + _gpsRefN + " = " + _heading);
}
public void DisplayCompass() {
GUI.Box(new Rect((Screen.width / 2) - 100, 20, 100, 25), _headingString);
}
}
You sir, are a god. This has been a problem for a couple days, I just found a duplicate component attached to the terrain for some reason.
Answer by robhuhn · Aug 30, 2013 at 01:48 PM
The script is definitely attached to only one gameObject in the scene? Does it flicker so may be _headingString is jumping from high values to low values immediately?