• 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 Setzer22 · Dec 27, 2012 at 07:51 PM · yieldframes

Pausing a script execution

Hello everyone

Let's say I have a loop instantiating blocks to form a map, and I don't want all the loop to happen in one frame, as it makes the program crash. Is there any easy way to pause the game execution for a couple of frames (let's say, instantiate 100 blocks on every frame rather than just trying to instantiate 1000 in one frame). I've done something like this, and, for testing purposes, I also added more delay, making each block spawn after 1 second.

The code looks like this:

 //Check the important edit below

So the point is this code isn't working at all. I've seen, though, that it causes refference errors when I want to iterate through a lot of blocks (more than 10,000) and it does nothing when working with a small 5x5 gameObject array. It seems I'm not implementing this right, so I'd like some advice on how it's done.

Thank you very much!

IMPORTANT EDIT:

Well, after some searching I've found a way which works for me, instead of making my method void, I made it return an IEnumerable object, and then used the "yield return WaitForSeconds(1.0f) inside of my code. Then, I call the metod which instantiates the map with the StartCoroutine function.

I've encountered some weird issues while doing this though:

As I said in the main post, this script instantiates blocks stored in an array. Those gameObject instances, come from another class, called AssetLibrary, in which I store all the block tiles classified by ID. So for some weird reason, the program works with small maps (i've tested up to 10x10) but it's not working for larger maps. The editor trows me this error message: "MissingRefferenceException: The variable blocks (this is the gameObject array where the map is referencing at) doesn't exist anymore"

The code looks like this:

 public class MapInstantiator : MonoBehaviour {
     
     public void ReadMap(string s) {
        // This reads the map from a text file and stores a refference for each tile of the array from another array stored in a class called AssetLibrary
     }
 
     public IEnumerator PrintMap() {
          for each block in map { //Pseudocoded
              Instantiate(block);
              return yield new WaitForSeconds(1.0f);
          }
     }
 }

And then in another class, i first call ReadMap and then i call StartCoroutine(PrintMap()).

It works fine for small maps, but it's not working for bigger ones, and I'm doing this so I'll be able to spawn larger maps so It's pointless to spawn a smaller one anyway.

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
1
Best Answer

Answer by Eric5h5 · Dec 27, 2012 at 08:31 PM

Making it wait a few frames won't make any difference. If it's crashing, then you are using too many objects, and spreading the instantiation over time won't help. You should be making the blocks using the Mesh class rather than instantiating zillions of separate objects (see any of the innumerable Minecraft clone topics). In general you don't want to use more than a few thousand objects at most, and fewer is better.

The literal answer to your question is that you are simply calling the wait function and not yielding on it or starting it as a coroutine, so it does nothing. See the docs for coroutines for how to use them properly. InstantiateMap would have to be a coroutine, and there is no point to the wait function at all since you'd just use WaitForSeconds inside InstantiateMap.

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 Setzer22 · Dec 27, 2012 at 08:38 PM 0
Share

Yes, I'm afraid I'll have to use the mesh class rather than using that many objects. Also, I should have done some more ressearch before posting the first question, as not longer before asking I found the answer by myself...

Anyway, thank you very much ^^

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to implement a delay between each time my gun fires (using coroutines or otherwise)?... 2 Answers

In c#, how to yield until a download is complete? 1 Answer

C# version of yield when loading a scene? 1 Answer

yield WaitForSeconds makes GUI button disappear 1 Answer

Wait for Coroutine without yield? (C#) 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