• 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 unity_N2KDSek8n06ERA · Jan 01, 2019 at 06:18 PM · loops

How can I use the while loop as a maintenance loop? (e.g.),How can I use the while loop as a maintenance-Loop? (e.g.)

   *void Start()
     {
         MaintenanceLoop();
         spawnVector = new Vector2(0, 0);
         playgroundSpawner = FindObjectOfType<PlaygroundSpawner>();
         blocksBehaviour = FindObjectOfType<BlocksBehaviour>();
 
         blockSpawner = GameObject.Find("BlockSpawner");
         if (!blockSpawner) 
         {
             blockSpawner = new GameObject("BlockSpawner");
         }
     }
 
     void Update()
     {
         playerVector.y = playgroundSpawner.playerPos.y;
     }
 
     public void MaintenanceLoop()
     {
         while(spawnVector.y == playerVector.y)
         {
             continue;
         }
         Spawn();
     }
 
     public void Spawn()
     {
         for (int i = 0; i < 15; i++)
         {
             if (spawnVector.y == playerVector.y)
             {
                 spawnVector.y += Random.Range(minSpawn, maxSpawn);
                 Vector2 currentPos = transform.position;
                 Vector2 yAdjusting = new Vector2(0, 0.5f);
                 int randValue = Random.Range(0, 3);
 
                 if (randValue >= 1)
                 {
                     GameObject SmallBlock = Instantiate(smallBlock, transform.position,
                         Quaternion.identity) as GameObject;
                 }
                 else
                 {
                     GameObject BigBlock = Instantiate(bigBlock, currentPos + yAdjusting,
                         Quaternion.identity) as GameObject;
                 }
             }
             else
             {
                 MaintenanceLoop();
             }
 
             if (i >= 14)
             {
                 Difficulty();
                 i = 0;
             }
         }
     }
         
     public void Difficulty()
     {   
         if (Random.Range(minSpawn, maxSpawn) > minYSpawnFrequency)
         {
             minSpawn--;
             maxSpawn--;
             blocksBehaviour.blockSpeed += 0.25f;
             MaintenanceLoop();*

My goal is that when the Player doesnt move the while Loop gets automatically called and works as an maintenance loop until it breaks when the spawnvector is equal the playerPos —> so the for loop can be executed the amount of times and so on

, **void Start() { MaintenanceLoop(); spawnVector = new Vector2(0, 0); playgroundSpawner = FindObjectOfType(); blocksBehaviour = FindObjectOfType();

         blockSpawner = GameObject.Find("BlockSpawner");
         if (!blockSpawner) 
         {
             blockSpawner = new GameObject("BlockSpawner");
         }
     }
 
     void Update()
     {
         playerVector.y = playgroundSpawner.playerPos.y;
     }
 
     public void MaintenanceLoop()
     {
         while(spawnVector.y == playerVector.y)
         {
             continue;
         }
         Spawn();
     }
 
     public void Spawn()
     {
         for (int i = 0; i < 15; i++)
         {
             if (spawnVector.y == playerVector.y)
             {
                 spawnVector.y += Random.Range(minSpawn, maxSpawn);
                 Vector2 currentPos = transform.position;
                 Vector2 yAdjusting = new Vector2(0, 0.5f);
                 int randValue = Random.Range(0, 3);
 
                 if (randValue >= 1)
                 {
                     GameObject SmallBlock = Instantiate(smallBlock, transform.position,
                         Quaternion.identity) as GameObject;
                 }
                 else
                 {
                     GameObject BigBlock = Instantiate(bigBlock, currentPos + yAdjusting,
                         Quaternion.identity) as GameObject;
                 }
             }
             else
             {
                 MaintenanceLoop();
             }
 
             if (i >= 14)
             {
                 Difficulty();
                 i = 0;
             }
         }
     }
         
     public void Difficulty()
     {   
         if (Random.Range(minSpawn, maxSpawn) > minYSpawnFrequency)
         {
             minSpawn--;
             maxSpawn--;
             blocksBehaviour.blockSpeed += 0.25f;
             MaintenanceLoop();**

My goal is that when the player doesnt move the while Loop gets automatically called and works as an maintenance loop until it breaks when the playerPos is equal the spawn Vector—> so the for Loop can be executed the amount of Times an so on.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Hellium · Jan 01, 2019 at 06:26 PM

Remove the MaintenanceLoop and do the check in the Update, which is ran once per frame

  void Start()
  {
      spawnVector = new Vector2(0, 0);
      playgroundSpawner = FindObjectOfType<PlaygroundSpawner>();
      blocksBehaviour = FindObjectOfType<BlocksBehaviour>();
 
      blockSpawner = GameObject.Find("BlockSpawner");
      if (!blockSpawner) 
      {
          blockSpawner = new GameObject("BlockSpawner");
      }
  }
  void Update()
  {
      playerVector.y = playgroundSpawner.playerPos.y;
      if( !Mathf.Approximately(spawnVector.y, playerVector.y) )
      {
          Spawn();
      }
  }
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 DownER149 · Jan 01, 2019 at 07:48 PM

StartCoroutine("Setup2"); }

  private IEnumerator Setup2()
 {
     yield return new WaitUntil(() => ***** Your Condition here*****);
    **** what you want to happen here*****

 }
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

98 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

Related Questions

ArrayList Loop Through - only new elements accessible? 0 Answers

c# loops not working yet no erors 1 Answer

How to destroy numbers after solving them? 2 Answers

Script to Tell if Circuit / Path is Complete 0 Answers

for loop error 1 Answer


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