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 Miaw666 · Oct 17, 2021 at 05:50 PM · function call

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!!

Comment
Add comment · Show 4
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 Hellium · Oct 17, 2021 at 05:56 PM 2
Share

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.

avatar image Miaw666 Hellium · Oct 17, 2021 at 09:20 PM 0
Share

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

avatar image Eno-Khaon Miaw666 · Oct 17, 2021 at 09:43 PM 0
Share

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");
Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

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.

Comment
Add comment · 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
0

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();
 }
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 Miaw666 · Oct 20, 2021 at 08:10 PM 0
Share

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

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

132 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image

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


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