Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Neptune_Amabie · Mar 23 at 11:46 PM · instantiatecoroutineloopwaitforsecondswhile-loop

Unity Freezing after for loop

Im working on a flappy bird clone to test my skills but my for loop causes the game to crash

The script makes a random position for obstacles if the player is alive and then instantiates them using the random position values with a 4 sec delay from my coroutine

But it freezes on launch please help

CODE:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class ObstacleMaker : MonoBehaviour { public GameObject player; public PlayerController playerScript; public GameObject topObstacle; public GameObject bottomObstacle;

 // Start is called before the first frame update
 void Start()
 {
     while (playerScript.isAlive == true)
     {
         StartCoroutine(loopDelay());
         Vector3 topPosition = new Vector3(15.65f, Random.Range(2.5f, 4.48f), 0f);
         Instantiate(topObstacle, topPosition, Quaternion.identity);
         Vector3 bottomPosition = new Vector3(15.65f, Random.Range(-3.72f, -4.58f), 0f);
         Instantiate(bottomObstacle, bottomPosition, Quaternion.identity);
     }
 }

 // Update is called once per frame
 void Update()
 {

 }

 IEnumerator loopDelay ()
 {
     yield return new WaitForSeconds(4);
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by rh_galaxy · Mar 24 at 12:02 AM

Two things.

Start() must be able to finish and exit on its own. If playerScript.isAlive is true when you enter the loop it will never become false because nothing else can set it. You must move this test to Update() which runs every frame.

loopDelay() does not run when you do StartCoroutine. Coroutines are added to a list to be run later. I always find it easier to do timing code directly in Update() myself like this:

 private float timer = 0.0f;
 void Update()
 {
     timer += Time.deltaTime;
     if(timer >= 4.0f)
     {
         timer = 0.0f;
         if(playerScript.isAlive == true)
         {
             Vector3 topPosition = new Vector3(15.65f, Random.Range(2.5f, 4.48f), 0f);
             Instantiate(topObstacle, topPosition, Quaternion.identity);
             Vector3 bottomPosition = new Vector3(15.65f, Random.Range(-3.72f, -4.58f), 0f);
             Instantiate(bottomObstacle, bottomPosition, Quaternion.identity);
         }
     }
 }
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 Neptune_Amabie · Mar 27 at 10:13 AM 0
Share

So if i put your code in place of my current one should it work or is this just a test?

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

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

Instantiate Script Not Working 1 Answer

Insantiate Loop Issue 0 Answers

How to make object fall loop? 0 Answers

running a while loop until coroutine finishes 0 Answers

Loop in coroutine stops after going halfway 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