Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 BlenderKiel97 · Jan 05, 2013 at 07:59 PM · buttonupdatestates

How can I check if I pressed F in the class other than Update()? C#

My question maybe is simple, but I have problem with this. I want to my script will send a message to GUIText called showText, which shows text on HUD. My problem is that: When I enter the trigger without pressed F (there is only "Use", which has F key applied to Input Manager), the message is right, but when I press it, the message is still the same. To show my expected message, I must exit the trigger and enter it again, but with hold down F key. This is I want to fix. How can I fix my script to show the other message when I press F key? There's my full code of DoorZone which uses somes variables in other script

 using UnityEngine;
 using System.Collections;
 
 public class DoorZone : MonoBehaviour {
     
     public bool doorIsOpenAlready = false;
     public int pointsRequired = 750;
     public GUIText textHints;
     
     bool UsePressed = false;
 
     void Start () {
     
     }
     
 
     void Update () {
         if(Input.GetButton ("Use")){
             UsePressed = true;
             //Debug.Log ("F is pressed!");
         }else{
             UsePressed= false;
             //Debug.Log ("F is not pressed!");
         }
     }
     
     void OnTriggerEnter(Collider col){
         if(col.gameObject.tag == "Player" && !doorIsOpenAlready){
             
             textHints.SendMessage ("ShowHint", "Would you like to open this door for "+pointsRequired+" points?");
             
             if(UsePressed){
             
                 if(Inventory.points >= pointsRequired){
                     textHints.SendMessage ("TurnOffText");
                     transform.animation.Play ("doorOpen");
                     doorIsOpenAlready=true;
                     Inventory.points = Inventory.points - pointsRequired;
                     Debug.Log(Inventory.points);
                 }else{
                     textHints.SendMessage ("ShowHint", "You haven't got enough money!");    
                 }
             }
         }
         
     }
     
     void OnTriggerExit(Collider col){
         textHints.SendMessage ("TurnOffText");    
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by sparkzbarca · Jan 05, 2013 at 08:06 PM

I BELIEVE you mean you wish to be able to press the button near it and get the popup.

OnTriggerEnter IS ONLY CALLED ONCE. WHEN YOU ENTER.

you should change it to OnTriggerStay it will keep running the code as long as they are in the trigger.

Good luck and mark as answered.

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 BlenderKiel97 · Jan 05, 2013 at 08:24 PM 0
Share

I can't believe it was very simple. Thank you so much for help and sorry for not the most polite language.

avatar image
0

Answer by Piflik · Jan 05, 2013 at 08:05 PM

Use two booleans. One you set to true OnTriggerEnter, the other OnKeyDown(KeyCode.F). If both are true, you change the message.

The problem is that OnTriggerEnter is only true for one frame. You could also use OnTriggerStay, but this uses more ressources.

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 BlenderKiel97 · Jan 05, 2013 at 08:25 PM 0
Share

Hmmm, I used OnTriggerStay and works fine. Thanks for help

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.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Button not update()ing,Button not updating 1 Answer

Best Way To Script State-Specific Behavior 1 Answer

Add EditorUI elements when a button is pressed 0 Answers

can't change bool in update function 1 Answer

in the update 5.6.1.f1, how to set source image for a button 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