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 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 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;
             }
         }
         }
     }
     
 }
 
Comment
Add comment
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
0
Best Answer

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

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
Add comment · 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.

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

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


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