- Home /
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.
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!
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);
}
Also, do what Seth suggested and use Application.dataPath if you plan on this working for anyone but yourself :)