- Home /
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;
}
}
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 :)
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
![](https://koobas.hobune.stream/wayback/20220613121120im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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