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 RickTick · Jul 29, 2012 at 07:50 PM · onguilabeldialogue

Changing displayed label in OnGUI from another function

Hi there,

I am creating a Dialogue-System. In OnGUI I am drawing a Label with the variable talkText in it.

I a function named talking() I am setting text to this talkText variable, getting the text from another object of the scene. The text is saved sentence by sentence inside an array. Now I am trying to assign the sentences one after another to talkText. But my OnGUI displays just the first sentence and after that nothing changes. Any idea?

 function OnGUI(){
    if(switchTalk){
 
       GUI.Label (Rect (Screen.width /2 , 5, 50, 33), talkText, style);
  
       talking();
  
    }
 }
     
 function talking(){
 
  // getting length of array, the number of sentences
  var talkLength : int = gameObject.Find(talkTo.name).GetComponent(dialogue).dialogOption1.Length;
 
     for(var talkCount : int = 0 ; talkCount < talkLength ; talkCount++ ){
 
     //this is where I change talkText, but in OnGUI() it's not affected
     talkText = gameObject.Find(talkTo.name).GetComponent(dialogue).dialogOption1[talkCount];
  
     yield WaitForSeconds (2);
  
     }
 
  talkTo = null;
  switchTalk = false;
  }
     
  
 
 
 In Update():
 
  if (hit.collider.tag == "NPC" && !switchTalk){
  
     if(Input.GetButtonUp("Fire1")){
  
        switchTalk = true;
        talkTo = hit.collider.gameObject;
  
  }

 
 
Comment
Add comment · Show 5
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 fafase · Jul 29, 2012 at 08:14 PM 0
Share

Personaly I prefer GuiText object, then you could have all your sentences in an array. Then when you press the mouse, you increase the index of the array and call only when you change the text

 guiText.text = str[i];
avatar image RickTick · Jul 30, 2012 at 07:27 PM 0
Share

Thank you very very much, works fine! :-)

avatar image whydoidoit · Jul 30, 2012 at 08:32 PM 0
Share

By the way your original problem was calling talking multiple times per frame - (from OnGUI) it was always initializing to the first string.

avatar image RickTick · Aug 02, 2012 at 01:32 AM 0
Share

I thought talking() is just called once, because I am turning it off with switchTalk after the first time ...

avatar image whydoidoit · Aug 02, 2012 at 01:34 AM 0
Share

Ah yes - in which case there was no guarantee it would ever draw - OnGUI is called multiple times per frame with different events - if logic/function calling like that can be a nightmare. Best to only do it if Event.current.type == EventType.repaint

0 Replies

· Add your reply
  • Sort: 

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making scrollbar for GUI.Label inside a draggable GUI.Window 0 Answers

Fade out GUILayout Area? 1 Answer

how to a count on the screen on UPDATE 1 Answer

How to use GUI.Label outside of "void OnGUI"? 0 Answers

Can't draw GUI.Label text on subpixel values 0 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