• 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 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
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
1

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
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
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 $$anonymous$$d 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.

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

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

Handling Movement - Animations 1 Answer

loop GUI Buttons doesn`t respond 0 Answers

SendMessage within a SendMessage Receiver 0 Answers

encapsulating if statement in update 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