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;
			}
		}
		}
	}
	
}

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.

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);
     }
   }
   }
}