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 straydogstrut · Apr 16, 2010 at 05:28 PM · instantiateguitextureguitext

Instantiating guiText / guiTexture

Further to my previous question about showing/hiding guiText, i'm working on a closed caption system where a caption will show when the player comes within range of a sound. I'm using a guiTexture to create the black background, and guiText for the caption String.

Most sounds in the environment are continuous, so they should show a caption all the while the player is in range, then hide the caption if not. However, a couple of sounds are one-shot played by animation events so I want these to show for a period of time specified by me.

I previously got a system working that would hide captions after a specified time, but as described above, this isn't exactly what I want. In trying to amend my code to hide captions if the player moves out of range, I've discovered that now only the one-shot events seem to show up reliably. I believe the reason being is that i'm using one guiText and guiTexture for the one-shot events, and another pair for the general sound effects. Since the general sound effects call the function from Update(), I think they are overriding each other.

Due to this problem, i'm not even sure if my revised IF statement is doing what I want it to now. Also, I'm thinking, rather than creating pairs of guiText/guiTexture for each caption, a better approach is to instantiate them. I know that they will require screen co-ordinates (in my case i'm using x 0.5 and y 0.2), but how would I use this with Object.Instantiate?

Here is my current code:

function showCaption () {

 // if this sound source is in range of the player
 if(inRange){
     // if we are not already showing a caption
     if(!textOn){
         myCaptionObj.guiText.text = myMessage;
         captionBox.guiTexture.enabled = true;
         textOn = true;

         // if this is a one-shot caption, show it for the specified time
         if(!isLooped){
             if(myDuration > 0){
                 yield WaitForSeconds(myDuration);
                 myCaptionObj.guiText.text = "";
                 captionBox.guiTexture.enabled = false;
                 yield WaitForSeconds(myDelay);
                 textOn = false;
             }
         } 

     }

 } else {

     // if we are out of range, clear the caption and hide the box
     myCaptionObj.guiText.text = "";
     captionBox.guiTexture.enabled = 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

1 Reply

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

Answer by Eric5h5 · Apr 16, 2010 at 06:41 PM

You instantiate them the same way as anything else:

var textObject : GUIText;

Instantiate(textObject); // or Instantiate(textObject, Vector3(.5, .2, 0.0), Quaternion.identity);

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 straydogstrut · Apr 16, 2010 at 11:07 PM 0
Share

Thanks Eric, I wasn't sure about the last parameter. Instantiating works fine now, but I need to get my head around destroying the object. I'm finding it more complicated since the above script is not on the caption guiText itself. I may just go ahead with my older script for the time being since i'm on a deadline.

avatar image Eric5h5 · Apr 17, 2010 at 12:36 AM 0
Share

@straydogstrut: To destroy an instantiated object, do var obj = Instantiate(textObject); and then Destroy(obj); when appropriate.

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

No one has followed this question yet.

Related Questions

GUITextures not lining up correctly 2 Answers

Multiplying GUIs 0 Answers

When I built my game in xcode all the gui textures/texts look different 1 Answer

Gui , Message , Text, Touch and Question ? 2 Answers

Dynamically instantiated game objects not showing in main camera (with background camera) 3 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