|
Hi, I have level0 (small) and level1 (large). I have the build set up so level0 loads first. I would then like to load an object (a door) into level0 when level1 had finished loading. I am using the Update() function to determine when level 1 has finished loading, but I find that if I try to instantiate the door from inside the Update() function, I get many many doors. Is there a way to get just one door? Here's the code. Thanks. Zaffer }
(comments are locked)
|
|
You'd be better off not using Update, but rather a coroutine, since the point of Update is that it runs every frame. With a coroutine you can just wait until the event is done and run the code afterward once.
(comments are locked)
|
|
Here's an exampe on how to use a coroutine: In this (really simple) example the coroutine gets started in the Start() function. You can do anything you like within a coroutine, the "yield" statement simply sets the function on "hold" for how long you choose. You can also use "yield return new WaitForEndOfFrame()" and other functions. When you get the hang of coroutines, you can do some really cool stuff with it. Cheers.
(comments are locked)
|
|
Hi, It took me awhile, but I finally figured out I could do what I wanted with a simple Boolean variable, doorPresent, which I set false in the Start() function. Then I set that to true after making only one door. Seems to be working so far. Coroutines and yield look really interesting -- will study them more. Thanks. Zaffer Well, this is merely a hack. It will work, but you're better off using the method I just posted. Greets
Jun 19 '12 at 02:33 PM
Tim Michels
(comments are locked)
|
|
Hi Eric5h5 and Tim, I wanted to take your advice about coroutines being better, and after some work, I was able to set up a loading sequence using a while loop, yield and coroutines instead Update(). I needed a little help from regular forum, but everything works. Here's the code. Zaffer P.S. You are right, Tim, couroutines are very cool! function Start(){ ManageLoadingElements(); }
(comments are locked)
|
|
I would use a boolean variable, that is true only for the first run. That's a good strategy too. Thanks.
Jul 03 '12 at 09:48 PM
Zaffer
But if you get more than one door, then that means the else branch isn't called as it should be, right?
Jul 03 '12 at 09:51 PM
Luci85
I have not actually tried your idea, just thought it looked like a good solution.
Jul 03 '12 at 11:33 PM
Zaffer
(comments are locked)
|

Thanks Eric5h5,
Would greatly appreciate it if you would show me how to set up the coroutine. Thanks.
Zaffer
Have you read the docs about StartCoroutine and so on?
Thanks for the tip. I will read up on coroutines and try to figure out.
Zaffer
Hi Tim and Eric5h5:
Still getting lots of doors. Am I getting warmer? Thanks. Zaffer
function Update() { if(Application.GetStreamProgressForLevel(levelToLoad) == 1) { Progress.message = "Level at index 1 has been fully streamed!"; StartCoroutine(MakeDoor()); level1Ready = true; } else { percentageLoaded = Application.GetStreamProgressForLevel(levelToLoad) * 100; Progress.message = percentageLoaded.ToString(); level1Ready = false; } }
function MakeDoor(){ yield Instantiate(door, Vector3(154, 1.8, 166), Quaternion.identity);
}
I think you don't need the Update() function.
I'll write something down here which I think is more like what your coroutine should look like: (sorry it's in C#)
IEnumerator LoadInOrder() { level1Ready = false;
You should start this coroutine after you started loading the level. Then, this coroutine will perform the while loop until it is fully loaded, whereafter your door will only be instantiated once.
Hope this is what you want to achieve.