Duplicate GUI.Label Instances
Hey there, I am using GUI.Label to indicate onscreen where asteroid fields are in the world.
The code I have works great, except for one thing. There are duplicate labels on screen, specifically two for each asteroid field. One label is in the correct spot, and one always seems to be onscreen where there is actually no asteroid field there. I figure it has something to do with 3D space and some asteroid fields not being onscreen when the text is made, but I'm not positive.
public class AsteroidTextHandler : MonoBehaviour {
public string text;
//private GUIStyle style = new GUIStyle();
private void OnGUI()
{
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
Vector2 tSize = GUI.skin.label.CalcSize(new GUIContent(text));
//style.fontSize = 12;
GUI.contentColor = Color.white;
GUI.Label(new Rect(pos.x, Screen.height - pos.y, tSize.x, tSize.y), text);
}
}
The text is passed through in my AsteroidHandler class and this script is a component of the asteroid field itself.
Answer by pqtbrown · Jun 11, 2017 at 03:22 AM
For those of you that run into this problem in the future with a 3D project, I figured out the solution and will have it written below. Basically, I checked to see if the object was at least 60 degrees into the main camera's field of view.
if (Vector3.Angle(Camera.main.transform.forward, transform.position - Camera.main.transform.position) < angle)
{
//label stuff here
}
Your answer
Follow this Question
Related Questions
OnGUI() problems in unity 5.6, worked with Unity 4. 0 Answers
Why is my GUI popup showing up for both host and client? 0 Answers
Auto resize/rescale ongui 0 Answers
How to change GUI label size 1 Answer
GUI Labels doesn't center anymore 0 Answers