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 gilgada · Oct 04, 2012 at 09:40 PM · updatestringdebugongui

Update the GUI when string changes

I have a class that creates a GUI Box to be filled with information. The information is read from a text file and converted into a string. I know this works because using Debug.Log(string) gives me the contents of the file in the console.

The problem is, the changes to this string are done in Update() and I want to use the string in a GUI.Box Can I update my OnGUI() method from Update()?

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
2
Best Answer

Answer by DannyB · Oct 04, 2012 at 10:36 PM

Are you reading a file on each iteration of the Update()? This sounds way too expensive, you should probably avoid that. But nevertheless, something like the below should work for you I hope:

 string text;
 
 void Update() {
     text = GetTextFromSomewhere();
 }
 
 void OnGUI() {
     GUI.Label( new Rect(...), text );
 }
Comment
Add comment · Show 11 · 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 DannyB · Oct 05, 2012 at 11:57 AM 1
Share

Is your code structured like the example I provided? Can you provide (add to question) the most relevant snippet of your code? (remove all but the lines that actually matter to the problem at hand).

avatar image DannyB · Oct 09, 2012 at 05:25 AM 1
Share

Well, the problem with this script is that the global variable "contents" is not what you populate in your Update() loop.

Change this:
string contents = ConvertStringArrayToStringJoin(allcontents);

To this:
contents = ConvertStringArrayToStringJoin(allcontents);

avatar image whydoidoit · Oct 09, 2012 at 06:36 AM 1
Share

BTW I don't see it resetting searchField.Confirm - make sure that is only valid for a single frame or this code will run every Update. You would probably be better of using Send$$anonymous$$essage when that condition is true rather than checking it in an Update loop.

avatar image DannyB · Oct 09, 2012 at 06:38 AM 1
Share

I agree. Checking for such things in the update loop makes me worried. A small misstep and you have a huge performance liability.

I wouldn't necessarily use Send$$anonymous$$essage, but I would use a method that does not require a test in Update().

avatar image DannyB · Oct 09, 2012 at 05:59 PM 1
Share

Buddy.... are you reading the answers? I know your debug.log prints it correctly, because it is in the same scope as your contents variable. The contents variable you are using in your Update loop is in local scope, and the one you are using in the OnGUI is your global field.

Do what I mentioned 4 comments ago, or follow the pattern in the answer and your problem is gone.

I have tested the script you have provided, and made the change I mentioned which solves it.

If you follow the solution and still have a problem, post again.

(and don't confuse yourself with Send$$anonymous$$essage now, it is not going to solve your problem).

Show more comments
avatar image
1

Answer by victorafael.com · Oct 05, 2012 at 12:02 AM

The changes on Update() will reflect on OnGUI() Method...

if this doesn't works... try to create a display string variable, displaying it on OnGUI() method and changing it on Update()

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

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

12 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

Related Questions

Return a string not Working in Unity 2 Answers

GetKeyDown is fired more than once in Update() 1 Answer

If(Application.isPlaying == "MainMenu") then Show A messenger. take a info 1 Answer

Debug.log in Update without crashing 1 Answer

OnGUI updates several times a frame. Why? 2 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