• 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
Question by BigDakaFa7 · Jul 04, 2013 at 09:28 PM · guitextupdatesavepath

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 $$anonymous$$t 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;
             }
         }
         }
     }
     
 }
 
Comment

People who like this

0 Show 0
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
Best Answer

Answer by trs9556 · Jul 04, 2013 at 10:35 PM

Ok so that means updater(); is actually being CALLED twice.

So I t$$anonymous$$nk 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 t$$anonymous$$s:

 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 t$$anonymous$$s:

  void updater()
 {
    if(chat)
    {
 
    if(File.Exists(path))
    {
     chat = false;
 
      using (StreamWriter sw = File.AppendText(path))
      {
       sw.WriteLine(stringToEdit);
      }
    }
    }
 }
Comment

People who like this

0 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 BigDakaFa7 · Jul 04, 2013 at 10:53 PM 0
Share

k Now it sends the message 4 times :( and the debug logs 4 times for each debug log all i want is one message :(

avatar image trs9556 · Jul 04, 2013 at 11:03 PM 0
Share

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..

avatar image

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.

Comment

People who like this

0 Show 5 · 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 BigDakaFa7 · Jul 04, 2013 at 10:09 PM 0
Share

that already happens but it happens twice. THe text document has two lines in in of what ever i typed in game.

avatar image trs9556 · Jul 04, 2013 at 10:15 PM 0
Share

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.

avatar image BigDakaFa7 · Jul 04, 2013 at 10:20 PM 0
Share

yeah it wrote the text twice in the debug log how do i fix that!!! i lowered it from 4 times to two

avatar image trs9556 · Jul 04, 2013 at 10:28 PM 0
Share

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.

avatar image BigDakaFa7 · Jul 04, 2013 at 10:29 PM 0
Share

twice each

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

GUI Text Area that Reads / Writes / Saves 2 Answers

Andoid enter text without gui 1 Answer

v4.6 Create GUI Elements Via Script? 1 Answer

Relate font size to screen size in GUISkin 2 Answers

Changing 3d text through script 1 Answer


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