I’m trying to display a GUI box at the location of some Transforms in a list. The error line is:
Vector3 thePosition = Camera.main.WorldToScreenPoint(trigger.position);
The list definitely has transforms inside it.
NullReferenceException
UnityEngine.Camera.WorldToScreenPoint (Vector3 position) (at C:/BuildAgent/work/f724c1acfee760b6/Runtime/ExportGenerated/Editor/Graphics.cs:632)
InteractableTargets.DisplayTargets () (at Assets/_Scripts/Managers/GUIManager/InteractableTargets.cs:87)
InteractableTargets.OnGUI () (at Assets/_Scripts/Managers/GUIManager/InteractableTargets.cs:73)
private bool isEnabled;
public List<Transform> interactables;
//=======================================
void OnGUI()
{
if (isEnabled)
{
DisplayTargets();
}
}
//=======================================
void DisplayTargets()
{
foreach(Transform trigger in interactables)
{
Vector3 thePosition = Camera.main.WorldToScreenPoint(trigger.position);
GUI.Box(new Rect(thePosition.x-(50/2), Screen.height-thePosition.y-(50/2), 50, 50), trigger.name);
}
}
You're right. It should have been "PlayerChar.PLAYER.camera.WorldToScreenPoint(trigger.position)". Thanks.
– Stardog