- Home /
Global Variable[Solved]
Ive made a significant amount of progress, I am able to get the object information in the Update function and I'm able to see the Vector3 using Debug.Log()..but I cannot get this updated information in the OnGUI() function is there a way to make the Vector3 into a global variable so I can access it in the OnGUI() function? Here is my code:
using UnityEngine;
using System.Collections;
public class text : MonoBehaviour {
public SimpleCloudHandler other;
public Transform target;
public Vector3 screenPos;
void Start(){
}
void Update () {
Vector3 screenPos = camera.WorldToScreenPoint (target.position);
Debug.Log (screenPos[0]);
}
void OnGUI() {
GUI.Label (new Rect(screenPos[0],screenPos[1],screenPos[2],50), "" + other.TextFunc());
Debug.Log (screenPos[0]);
}
}
Thanks!
Answer by Graham-Dunnett · Jul 09, 2014 at 09:17 PM
GUI uses screen space, transform is in 3d space. See http://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html.
Ive made a significant amount of progress, I am able to get the object information in the Update function and I'm able to see the Vector3 using Debug.Log()..but I cannot get this updated information in the OnGUI() function is there a way to make the Vector3 into a global variable so I can access it in the OnGUI() function? Here is my code:
using UnityEngine;
using System.Collections;
public class text : $$anonymous$$onoBehaviour {
public SimpleCloudHandler other;
public Transform target;
public Vector3 screenPos;
void Start(){
}
void Update () {
Vector3 screenPos = camera.WorldToScreenPoint (target.position);
Debug.Log (screenPos[0]);
}
void OnGUI() {
GUI.Label (new Rect(screenPos[0],screenPos[1],screenPos[2],50), "" + other.TextFunc());
Debug.Log (screenPos[0]);
}
}
Thanks!
You need to remove the 'Vector3' from the beginning of line 13. You are declaring 'screenPos' twice. Once at at the class level, and once local to the Start() function. The one in the Start() function is 'hiding' the one at the class level. If you remove the Vector3 from the beginning of the line, then 'screenPos' will refer to the one at the class level and be availabe in OnGUI().
Your answer
Follow this Question
Related Questions
GUI item with object position+dimensions 0 Answers
uGUI keep position and size of GUI elements when anchors change 3 Answers
Merging Object 1 Answer
Lerp a Object along X and Z axis only. 3 Answers
Controlling visibility of several objects with GUI 2 Answers