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 SEDBC3 · Nov 07, 2013 at 07:01 PM · buttonframerateexternalgetkeygetkeyup

Problems with GetKeyUp generating text outside Unity

I'm trying to record button presses in a document outside of Unity. The presses are being recorded, but I only want it to print once for every time the button is pressed. I thought using Input.GetKeyUp in Update() would accomplish that, but I'm still getting several outputs for one single button press. This is the code I have now. Can you see anything wrong with it or do you have any suggestions for how I can do this? Thanks!

 private string directionChosen;
     
     void Update () {
         string path = @"/Users/saradiaz/museum/Log/responses";
         //string path = @"C:\Users\ccn\Desktop\Sara\train\Log\coords";
         if (Input.GetKeyUp("right")) {
             directionChosen = "\nright";
         }
         if (Input.GetKeyUp("left")) {
             directionChosen = "\nleft";
         }
         if (Input.GetKeyUp("up")) {
             directionChosen = "\nstraight";
         }
         System.IO.File.AppendAllText(path, System.String.Format("{0}", directionChosen));
             
     }
 }


P.s I've also tried KeyCode.UpArrow etc instead of the strings.

Comment
Add comment · Show 1
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 Seth-McCumber · Nov 07, 2013 at 07:04 PM 0
Share

First Change The path string To Application.dataPath+"/filename.txt" So It Will Work On All Computers. Second You Cant Use "Get$$anonymous$$eyUp" For This. Use Get$$anonymous$$eyDown. Third You Cant Use "left" Or "right" Or Any Of The That. You Can Use Input.GetButtonDown, Or Use $$anonymous$$eyCodes. Hope This Helps!

1 Reply

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

Answer by drod7425 · Nov 07, 2013 at 07:18 PM

Well, you're writing to the file in the Update function. It's writing directionChosen to coords every frame. While there are many other things wrong with this script, I think that answers why you're getting multiples.

Oh fine, try this:

 public string path; //You could set this is the inspector if you want
 
 void Start(){
     path = "C:\Users\ccn\Desktop\Sara\train\Log\coords.txt";
 }
 
 void Update () {
     if(Input.GetKeyUp(KeyCode.RightArrow)) {
         WriteDirection("right");
     }
     if(Input.GetKeyUp(KeyCode.LeftArrow)) {
         WriteDirection("left");
     }
     if(Input.GetKeyUp(KeyCode.UpArrow)) {
         WriteDirection("straight");
     }
 }
 
 private void WriteDirection(string direction){
     System.IO.File.AppendAllText(path,"\n"+direction);
 }
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 drod7425 · Nov 07, 2013 at 07:20 PM 0
Share

Also, do what Seth suggested and use Application.dataPath if you plan on this working for anyone but yourself :)

avatar image SEDBC3 · Nov 07, 2013 at 07:34 PM 0
Share

Worked! Thanks!

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

17 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

Related Questions

Refactor code from getting input from keyboard to get input from UI Buttons 1 Answer

Input.GetKey question 1 Answer

Update GetKey If Statements both playing instantly 1 Answer

Dialogue with GetKey - Codes 1 Answer

Change view if key is pressed 4 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