Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Grady · Jun 28, 2011 at 07:01 AM · guiobjecttexttime

How to draw GUI Text from code

Hey guys,

I was wondering, how do you draw some GUI text (or any GUI object for that matter) on the screen from Javascript. I know how to use the Time.time method to display it after a number of seconds but I want to be able to remove it after a number of seconds as well....

And also, it would be good if I was able to choose the font that this GUI text would use!!!!! Thanks

-Grady

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Jesus_Freak · Jun 28, 2011 at 07:35 AM

I recommend using an array of strings for your subtitles.

 var waitTime : float;
 var seconds : int; 
 var timer : float;    
 var f : Font;
 var rectplayer1 : Rect[];
 var rectplayer2 : Rect[]; //just keep making new arrays for every amount of subtitles you want.
 var s1 : String[];
 var s2 : String[]; //same here
 var cur1 : int;
 var cur2 : int; //and here
 
 function Update() {
     waitTime = (Time.deltaTime * seconds);
     while(timer < waitTime) {
         timer += Time.deltaTime;
     }
     if(timer >= waitTime) {
         cur2++;
         timer = 0;
     }
 }
 
 //vars and function for gui code here in the same script:
 
 
     
 function OnGUI() {
         GUI.skin.label.font = f;
         GUI.Label(rectplayer1[cur1],s1[cur2]); //change cur1 between two or three rects and continuously increment cur2 to get an illusion of dialogue.
 }



See? make one GUI.Label for each character's subtitle you want, with its own string array, and it's own original cur value and after that character is "done talking," increment cur to go to the next subtitle. And in case you don't know already, to use an array, set the size to however many elements (vars) you want, and then set those values to whatever you want, via the inspector, or code.

If you have any more questions, leave another comment. :) I hope I helped!

Comment
Add comment · Show 18 · 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 Grady · Jun 28, 2011 at 08:21 AM 0
Share

O$$anonymous$$, thanks very much for the examples!!!! :D i've managed to get the first one to work, but am having trouble with the second. although, as far as i understand, you would attach the second example to a GUI object. With my game, I am trying to make it have lots of different text come up on the screen at different times, as it will be subtitles for other characters speech in the game. I was thinking, that probably the most efficient way to achieve this would be to use the first method, but to set it so that it stops drawing the GUI.... would this be possible

-Grady

avatar image Jesus_Freak · Jun 28, 2011 at 09:20 AM 0
Share

And to answer, yes you'd attach the original posted second script to a GUI object, preferably a GUI Text object. I elaborated on the first because JavaScript in unity can make it's own GUI Elements and edit the from a code, which can do a lot more than a builtin GUI Object.

avatar image Grady · Jun 28, 2011 at 11:35 PM 0
Share

when i just put in the first script, under rect it has no x or y, it just has "Size 0"???

avatar image Jesus_Freak · Jun 28, 2011 at 11:39 PM 0
Share

yeah, the [] after Rect and String makes them an array. so set the size to however many subtitles each character has. like if it has 4, set it to 4. you should then get 4 Elements with a down arrow, to open up as 4 editable Rects. and Strings.

avatar image Grady · Jun 28, 2011 at 11:39 PM 0
Share

oh, stupid me, you have to put a number in there first!!!!!! :P so in the rect size element, would i put the number of different subtitles that will appear????

i managed to get one string to display, but i can't get it to display two, or have one, and then change to the next????

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Scrolling Text 1 Answer

Making a script for all gameobject GUITEXT. 1 Answer

Gui text script - help 1 Answer

Delay Gameplay While Displaying 3,2,1 go!!! 3 Answers

GUIText not working properly with a timer. 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