• 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
Question by ScottyKal · Apr 30, 2015 at 06:22 PM · guiupdateloopif-statementssendmessage

Why is OnGUI not working?

OnGui in not working if I have the Hover function updated in Update. The print shows that showLabel is changing from false to true as I hover over the object but the label will not come up. Here are the two scripts that are talking to each-other:

Interactee.js

 var Interactable : boolean = true;
 
 private var showLabel : boolean;
 
 function Update(){
     print (showLabel);
     Hovers(false);
 }
 
 //Is the player interacting with the this object???
 function Interact(Interacting : boolean){
     if (Interactable){
         if (Interacting){
             //Action();
         }
     }
 }
 
 function Hovers (Hovering : boolean){
     if(Hovering){
         showLabel = true;
     }else{
         showLabel = false;
     }
 }
 
 //The Action you want to happen...
 /*function Action(){
     //Make the item a child of backpack...
     this.transform.parent = backpack.transform;
 }*/
 
 //While hovered show something...
 function OnGUI(){
     if (showLabel){
         GUI.Label (Rect (Screen.width*0.5,Screen.height*0.65,100,50), "Hovering");
     }
 }
 

Interactor.js

 var distance = 10;
 var Interacting : boolean = true;
 var Hovering : boolean = true;
 
 //When "Interact" button is pressed do function Interact... 
 function Update(){
     Hover();
     if (Input.GetButtonDown("Interact")){
         Interact();
     }
 }
 
 //Raycast
 function Interact(){
     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 20));
     
     if (Physics.Raycast (ray, hit, distance)){
         hit.transform.SendMessage("Interact", Interacting, SendMessageOptions.DontRequireReceiver);
     }
 }
 
 //Raycast
 function Hover(){
     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 20));
     
     if (Physics.Raycast (ray, hit, distance)){
         hit.transform.SendMessage("Hovers", Hovering, SendMessageOptions.DontRequireReceiver);
     }
 }
Comment

People who like this

0 Show 0
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

Answer by tanoshimi · Apr 30, 2015 at 07:07 PM

You're setting showLabel to false in every frame on line 7 of Interactee.js.

Comment
ScottyKal

People who like this

1 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
avatar image ScottyKal · Apr 30, 2015 at 09:34 PM 0
Share

When I hover over the object with that script it changes to true according to the console.

Edit: Never mind I see the print was before the Hover.

How do I get showLabel to go back to false after I stop hovering?

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How to call unity event once every time my object reach destination in Update()? 1 Answer

Change Button GUIStyle on Click 1 Answer

Trigger animation-function in Update 0 Answers

How to slow down large code? 1 Answer

Checking if statement in higher frequency 0 Answers


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