I’m doing a timed popup system which use a if to compare what time is it and my time limit (if time>= T).
If the condition is right, I start a coroutine and I want to add 25 sec at my time limit (T) at the end of my coroutine.
so
if (time >= T)
start coroutine;
Coroutine …{
A = true
yield return new waitforseconds
A = false
T = T +25;
}
Right now it just add 25 sec everyframe which is useless…
I need to reuse this coroutine. I want do everything (A= …) in my coroutine as much as possible but everytime the time reach the limit, launch the coroutine and add 25 sec at the limit and after these 25 sec do again the coroutine and add 25 sec. but right now it just keep adding my 25sec every frame.
Additionally, whatever you put before the “while” line will be executed only once when SomeCoroutine is called. If you’re planning to call SomeCoroutine multiple times, you should consider making separate function calls. If what you’re trying to do isn’t directly tied to what’s done in the coroutine, it probably shouldn’t be there in the first place.
It would help if you formatted your code using the code tags, like so:
then I’ve got these pieces of code duplicated with Pop2, Tpop2,…Pop3,…
With Tpop1 = 10 Tpop2 = 20 Tpop3 = 30 and popduration = 5
So display a popup after 10 sec which stays 5 sec then nothing during 5 sec then the popup 2 shows up for 5 sec, etc…
And when I’m at the last one, the last coroutine reactive BPopup1 (like this I’ll never have two popups at the same time.
so right now when I reach the limit of the last popup, (Tpop5) it doesn’t use the limits any more and display each of them one after another without any time between two of them.
So mainly what I want to reach is an automatic display of popups, configurable by time in sec (so with the limits Tpop as public int), and it needs to loop.
My whole coroutine needs to be done once per call, right know as it’s in the update is being called each frame as I can imagine… I learn the coding in self teaching when i started unity a year ago so i just don’t know the way of doing this I’ve done it as I feel to be right but it seems that it isn’t…
Let me know if it’s not understandable (English isn’t my first language).
Reconsider why you’re using Update at all. You said you wanted to loop through things, so why not use an array of text?
public float duration;
public float delay;
public string[] popupText;
private bool showPopup = false;
private int popupIndex;
function Awake ()
{
StartCoroutine (IncrementPopups ());
}
private IEnumerator IncrementPopups ()
{
while (popupIndex < popupText.Length)
{
yield return new WaitForSeconds (delay); // Waits the desired amount of time between popups.
showPopup = true;
yield return new WatiForSeconds (duration); // Waits the desired amount of time the popup should appear on-screen.
showPopup = false;
popupIndex++;
}
}
void OnGUI ()
{
if (showPopup)
{
GUI.Label(new Rect(PosX, PosY, Sx,Sy), popupText[popupIndex]); // Will show a label with the current popup text
}
}
Thank’s a lot, I’ll try that this afternoon, It seems more logical. That’s my main problem in scripting I don’t use enough the loops, I think I could optimize a lot my script with these…