- Home /
Wait until one function is done, THEN do the other function?
Hello! I'm trying to make a randomly generated map, though I'm relatively new to writing hard code, therefore I did it the very simple, but bad, way. This is my code:
void stepOne()
{
for (int i = 0; i < amountOfRemoval; i++)
{
Destroy(walls[Random.Range(0, walls.Length)].gameObject);
}
}
void stepTwo()
{
for (int i = 0; i < deleters.Length; i++)
{
if (Physics.CheckBox(deleters[i].transform.position, Vector3.one / 2, Quaternion.identity, miaw))
{
Destroy(deleters[i].transform.parent.gameObject);
}
}
}
void stepThree()
{
walls = new GameObject[0];
walls = GameObject.FindGameObjectsWithTag("wall");
player.transform.position = walls[Random.Range(0, walls.Length)].transform.position + new Vector3(2.1f, 1f, 2.1f);
player.transform.rotation = Quaternion.identity;
}
IEnumerator doIt()
{
stepOne();
yield return new WaitForSeconds(2);
deleters = GameObject.FindGameObjectsWithTag("deleter");
yield return new WaitForSeconds(1);
stepTwo();
yield return new WaitForSeconds(2);
stepThree();
}
This code does work, but it has high possibilities to cause a lot of extreme bugs. In the IEnumerator doIt() I am making the code wait between each step. Though I am making it wait by time (seconds). What I want is for stepTwo to start running only AFTER stepOne is completely done. And stepThree AFTER stepTwo is done. The problem is that if stepOne needs more than 2 seconds to finish, stepTwo will start when it shouldn't.
I'm almost 100% sure this is not how you're supposed to do it, but I don't know any other method.
Thank you!!
The problem is that if stepOne needs more than 2 seconds to finish, stepTwo will start when it shouldn't
Code execution is linear in your case. You aren't using any multithreading so the situation you're describing is purely impossible.
I don't know what multithreading is but I fixed the "problem" by making the "i" in each loop a public variable (in the stepOne the i in the loop is replaced with public int One, and so on). Then I only start the stepTwo if One in the loop in stepOne is equal to its max limit.
So in this case, stepTwo starts when One = amountOfRemoval.
I don't know if this will be useful to someone else, but I thought I'd share the "idea" just in case. Thank you! <3
Multithreading is when you have two separate... well, threads... running simultaneously and independently from each other. Of note, that would basically be a step beyond using Coroutines, which run at somewhat arbitrary intervals, but are still running on the main thread.
For example, Unity's main gameplay loop (i.e. EVERYTHING in your scripts (basically), general rendering, etc.) all takes place on a single thread. In a nutshell, it keeps things ordered and organized, so it's as beginner-friendly as possible.
As for how you "fixed" your "problem"... that sounds like a placebo. The scope of the variable ( i) doesn't really matter if you always reset it to the same starting value during sequential usage.
Really, this brings up a more important question:
What are the "extreme bugs" you're experiencing? Do things stop working when you run into errors? (I can easily imagine encountering NullReferenceExceptions, especially during "Step One")
Also, a quick side note -- this line's rather unnecessary...
walls = new GameObject[0];
... since the following line will set the array to the following function call's output...
walls = GameObject.FindGameObjectsWithTag("wall");
Answer by ChefMe727 · Oct 17, 2021 at 10:16 PM
You could IEnumerator and coroutines. I have only done this once and I am not that good at these but you can find documentation and answers on coroutines.
Answer by whydoidoit · Oct 17, 2021 at 10:30 PM
Your actual routines are synchronous here, so the coroutine isn't doing very much apart from waiting for nothing! I'm not 100% sure what you want to do, but right now, you could just have a function that did the three steps with no coroutine and it would work.
stepOne, stepTwo etc might take a while, but as they are synchronous then they will finish before the next thing happens.
void doIt()
{
stepOne();
stepTwo();
stepThree();
}
I did try that, but it somehow mixed up the order (or at last stepTwo didn't get called ONLY when stepOne was finished). If any step is called before the one prev is completely finished, the code will break. It's a very (very very) simple random map generator, so the steps HAVE to be in order.
I did "fix" it though, I wrote what I did in the comments of this question if you're interested though it's not a fix, just a solution if that makes sense.
Thank you though! :)
Your answer
Follow this Question
Related Questions
prober use of navmeshagent.stop()? 0 Answers
function calls for each frame in the scene 0 Answers
How to pass a vector3 as arguement to an IEnumerator in another script? 2 Answers
How to call a function on a prefab when dragging it into a scene? 0 Answers
Running a script without being attached to an object? 2 Answers