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 BobbleHead · Nov 02, 2013 at 02:13 PM · yieldifwhilefor

Exiting an if statement when condition has been met

This has been bugging me for a little while, as the way I have incorporated this is wrong, well at least it can be done better.

The Player on first collision, meets a condition and a story is told. On second collision I want this story to be skippable. However, with long yield statements, it's not very responsive.

I have tried break; with while loops, but that didn't work as expected as if the player didn't press skip, then it would loop, woops!.

As always, I'm looking for a better solution.

Code:

     function OnTriggerEnter (other : Collider) {
         if(other.gameObject.tag =="Player"){ //has player finsihed introduction?
                if(object == 2){
                    PC.canmove = false;
                     
                     if(skip == false){
                         Debug.Log("Skip is false");
                         Debug.Log("a")
                                 yield WaitForSeconds(5);
                     Debug.Log("b")
                     yield WaitForSeconds(12);
                     Debug.Log("c")
                     yield WaitForSeconds(4);
                     Debug.Log("d")
                     yield WaitForSeconds(4);
                     Debug.Log("e")
                     yield WaitForSeconds(2);
                     Debug.Log("f")
                     yield WaitForSeconds(3);
                     Debug.Log("g")
                     yield WaitForSeconds(8);
                     Debug.Log("h")
                     skip = true;
                 }
                 
                 else{
                 
                     if(skipped == false){Debug.Log("1");if(skipped == false){yield WaitForSeconds(5);}}
                     if(skipped == false){Debug.Log("2");if(skipped == false){yield WaitForSeconds(12);}}
                     if(skipped == false){Debug.Log("3");if(skipped == false){yield WaitForSeconds(4);}}
                     if(skipped == false){Debug.Log("4");if(skipped == false){yield WaitForSeconds(4);}}
                     if(skipped == false){Debug.Log("5");if(skipped == false){yield WaitForSeconds(2);}}
                     if(skipped == false){if(skipped == false){yield WaitForSeconds(3);}Debug.Log("6");}
                     if(skipped == false){if(skipped == false){yield WaitForSeconds(8);}Debug.Log("7");}
                 }
                 gameObject.active = false;
                 PC.canmove = true;
                 skipped = false;
                }
            }
     }
     
     function OnGUI(){
             
                 if(skip == true){
                     // GUI.DrawTextureWithTexCoords(Rect(20, Screen.height-150, 128,128), skipbutton, Rect(0.125f,0f,0.125f,1f), true); 
                     if(GUI.Button(Rect(0,0,200,50), "SKIP")){
                     skipped = true;
                     }
                     //skipped = 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

Answer by zero3growlithe · Nov 02, 2013 at 03:02 PM

I'd recommend replacing those "yield"s with a single co-routine that would take care of the dialogues, something like this:


 private var skip : boolean;
 private var skipped : boolean;
 function Start (){
     dialogues = ["Go look for a sword...", "It should be somewhere around here.", "Don't you complain about its pink color!", "My wife wanted it that way..."];
     delaysTable = [3f, 5f, 5f, 4f];
 }
 function OnTriggerEnter (other : Collider){
     if (other.gameObject.tag == "Player" && dialogueTimer == 0){
         PlayDialogue();
     }
 }
 var dialogues : String[];
 var delaysTable : float[];
 private var dialogueProgress : float;
 private var dialogueTimer : float;
 private var firstTimePlay : boolean;
 function PlayDialogue (){
     dialogueProgress = 0; dialogueTimer = 0;
     while (dialogueProgress < delaysTable.length){
         print (dialogues[dialogueProgress]);
         if (firstTimePlay && skipped){
             dialogueTimer = 0;
             dialogueProgress = 0;
             break;
         }
         dialogueTimer += Time.deltaTime;
         if (dialogueTimer >= delaysTable[dialogueProgress]){
             dialogueProgress++;
             dialogueTimer = 0;
         }
         if (dialogueProgress == delaysTable.length - 1){
             firstTimePlay = true;
         }
         yield;
     }
 }
 function OnGUI(){
     if(skip == true){
         // GUI.DrawTextureWithTexCoords(Rect(20, Screen.height-150, 128,128), skipbutton, Rect(0.125f,0f,0.125f,1f), true); 
         if(GUI.Button(Rect(0,0,200,50), "SKIP")){
             skipped = true;
         }
         //skipped = false;
     }
 }

where:

  • "dialogues" holds all dialogues strings to display

  • "delaysTable" keeps info about how long should each line be displayed

  • PlayDialogue function uses a while loop to create an co-routine that runs independently, you can check whether the dialogueTimer is larger than 0 to make sure that player won't be able to move while dialogue plays

You can edit the times and dialogues in Unity's inspector.

Hope it works and helps since I didn't test it :)

Comment
Add comment · Show 1 · 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 BobbleHead · Nov 02, 2013 at 03:28 PM 0
Share

Very nice solution, the only issue I have is that I don't want them to show for that long, the yield statements were merely a pause before the next text is displayed. There is a background script controlling the display of any text, I just wish for the text to have the set yields between them before the next shows.

I will have a think, thank you!

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

16 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

Related Questions

Is this possible ? 1 Answer

Have a function wait unitl a boolean is false 1 Answer

Using Loops 1 Answer

Read Array Inside Array 0 Answers

How can I have a while(www is loading) loop? 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