- Home /
My text document is updating twice when its only supposed to once
Ok so i am currently working on a chat log that when i type a string and hit enter is saves it to a txt on my desktop. That works but it does it twice, i have idea why it does but i cant fix it to happen only once. I understand that the OnGui function works like void Update, so how can i make it only send one time please help. Heres the code using UnityEngine; using System.Collections; using System.IO; using System.Text; [RequireComponent (typeof(GUITexture))]
public class ChatWindowPlacement : MonoBehaviour {
public GUIStyle style;
private GUITexture Image;
private int Screen_width;
private int Screen_height;
public string stringToEdit = "";
public TextMesh speechBubble;
public bool chat = false;
private string path = @"C:\Users\Fa7\Desktop\chatLog.txt";
bool userHasHitReturn = false;
void Start () {
Screen_width = Screen.width;
Screen_height = Screen.height;
Image = guiTexture;
Image.pixelInset = new Rect (Screen_width / 100, Screen_height / 100, Image.pixelInset.width, Image.pixelInset.height);
chatUpdate();
}
// Update is called once per frame
void Update () {
}
void chatUpdate()
{
}
void OnGUI() {
stringToEdit = GUI.TextField(new Rect(Screen.width /60f,Screen.height / 1.05f, 300,20),stringToEdit,25);
Event e = Event.current;
if(e.keyCode == KeyCode.Return)
{
speechBubble.text = stringToEdit;
chat = true;
}
else if(chat)
{
updater();
}
GUI.Box(new Rect(10,10,100,100),stringToEdit);
if(GUI.Button( new Rect(Image.pixelInset.x + Image.pixelInset.width / 1.1f, Screen_height - Image.pixelInset.y - Image.pixelInset.height + Image.pixelInset.height/ 20, Image.pixelInset.width / 15, Image.pixelInset.height / 10) , "", style) ){
Debug.Log("Closing Chat");
gameObject.SetActive(false);
}
}
void updater()
{
if(chat)
{
if(File.Exists(path))
{
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(stringToEdit);
chat = false;
}
}
}
}
}
Answer by trs9556 · Jul 04, 2013 at 10:35 PM
Ok so that means updater(); is actually being CALLED twice.
So I think that the OnGUI is calling a couple frames (probably 2 because you get 2 lines) in the time it takes updater to work, therefore it's getting called twice.
So change your gui to this:
void OnGUI() {
stringToEdit = GUI.TextField(new Rect(Screen.width /60f,Screen.height / 1.05f, 300,20),stringToEdit,25);
Event e = Event.current;
if(e.keyCode == KeyCode.Return && !chat)
{
speechBubble.text = stringToEdit;
chat = true;
updater()
}
GUI.Box(new Rect(10,10,100,100),stringToEdit);
if(GUI.Button( new Rect(Image.pixelInset.x + Image.pixelInset.width / 1.1f, Screen_height - Image.pixelInset.y - Image.pixelInset.height + Image.pixelInset.height/ 20, Image.pixelInset.width / 15, Image.pixelInset.height / 10) , "", style) ){
Debug.Log("Closing Chat");
gameObject.SetActive(false);
}
}
And just for safe measures change your updater method to this:
void updater()
{
if(chat)
{
if(File.Exists(path))
{
chat = false;
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(stringToEdit);
}
}
}
}
k Now it sends the message 4 times :( and the debug logs 4 times for each debug log all i want is one message :(
Oops my bad. I deleted your if statement and never re-added it. Try the above now.
The reason why it was executing 4ish times is because in the time you were taking to hit enter, 4 frames of the OnGUI were being called all while the enter key was pressed..
Answer by Graham-Dunnett · Jul 04, 2013 at 09:30 PM
OnGUI
is called multiple times per frame, once for each event that needs to be handled. I guess you can dump the text out when the user presses return.
that already happens but it happens twice. THe text document has two lines in in of what ever i typed in game.
Oh ok I see, didn't realize that is what you meant.
On line 60 add this line
Debug.Log("going to write: "+stringToEdit);
The problem might not be in the actual writing, the variable it's writing might actually have it twice.
And also you can add this to line 66
Debug.Log("We have written to the file");
and see if "we have written to the file" gets executed twice.
yeah it wrote the text twice in the debug log how do i fix that!!! i lowered it from 4 times to two
Ok so on line 60 add
Debug.Log("debug1");
And on line 66 add
Debug.Log("Debug2");
tell me how many times debug1 and debug2 are printed.
Your answer
Follow this Question
Related Questions
GUI Text Area that Reads / Writes / Saves 2 Answers
How do I get my GUI text to open on awake? 1 Answer
GUI Text Score counter 3 Answers
Saving a UI Text Number Multiple Times 2 Answers
Gui Text Script 4 Answers