• 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 Wooleira · Feb 13, 2014 at 12:22 PM · javascriptgui textphysics2d.raycast

In UnityScript, why does GUI not get called within hit on collider?

I'm trying to print out text when a user touches a sprite, however, even though I get no errors when building and running the code, the code refuses to printout the text and I cannot understand what I am doing wrong. Please help me understand why it does not print. It prints the debug text when I touch the sprite, but not the GUI text with total.

This is the code:

 #pragma strict
 
 function Start () {
 
 
 OnGUI();
 }
 
 function OnGUI(){
 //var total = 0;
 
     //GUI.Label( myRect, total.ToString() ); // Displays "10".
     //GUI.Label( myRect, "" + bullets ); // Displays "10".
    // GUI.Label(Rect(0,0,Screen.width,Screen.height),"Total:"+total);
 }
 //function Update () {
 
 //}
 var platform : RuntimePlatform = Application.platform;
 
 function Update(){
 
     if(platform == RuntimePlatform.Android || platform == RuntimePlatform.IPhonePlayer){
        if(Input.touchCount > 0) {
          if(Input.GetTouch(0).phase == TouchPhase.Began){
           checkTouch(Input.GetTouch(0).position);
          }
        }
     }else if(platform == RuntimePlatform.WindowsEditor){
        if(Input.GetMouseButtonDown(0)) {
          checkTouch(Input.mousePosition);
        }
     }
 }
  
 function checkTouch(pos){
 var total = 0;
 
     var wp : Vector3 = Camera.main.ScreenToWorldPoint(pos);
     var touchPos : Vector2 = new Vector2(wp.x, wp.y);
     var hit = Physics2D.OverlapPoint(touchPos);
  
     if(hit){
     GUI.Label(Rect(0,0,Screen.width,Screen.height),"Total:");
        Debug.Log(hit.transform.gameObject.name);
        hit.transform.gameObject.SendMessage('Clicked',0,SendMessageOptions.DontRequireReceiver);
       
      
       //total = total +1;
     }
 }
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
Best Answer

Answer by nasa8 · Feb 13, 2014 at 12:38 PM

If I'm not mistaken you should display GUI elements only in OnGUI but not in Update. Move GUI.Label... to OnGUI and just assign the text you want to display in your "touch detection" function to a global variable which you'll access then from OnGUI

Comment
Add comment · Show 1 · 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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Life Bar System Help 1 Answer

TextAsset inside the Array - UnityScript/JavaScript 1 Answer

Flashlight Script Help...? 2 Answers

how to use / simulate a using declaration in unityscript 1 Answer

Try Catch not executing. 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges