• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by fredyyanez · Jul 09, 2014 at 09:15 PM · guitransformobject

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fredyyanez · Jul 10, 2014 at 12:05 AM 0
Share

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!

avatar image robertbu · Jul 10, 2014 at 04:28 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GUI item with object position+dimensions 0 Answers

NGUI- lerping widget on Y Axis only working in one direction 0 Answers

pragma strict convert object error 2 Answers

How to create skills and drag them to slots. 2 Answers

Move hit.point 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges