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 Yinoguns · Aug 27, 2014 at 11:23 PM · coroutineloopsoutputstalling

Sequential Coroutines sometimes halt

Hello, I've been working on improving my Dissertation Project from University.

This game creates a bunch of rooms, so ive been implementing Coroutines to create the map over time, rather than just in methods executed from the Start method.

Problem is majority of the time, all the map is created; dandy.

However sometimes only a percentage of it is, and its different each time.

Here is my output:

 Not Desktop Application
 MapModeManager.start
 About to run CoR - createBlankMap
 Creating Blank Room: 0, 0
 Creating Blank Room: 0, 1
 ...
 Creating Blank Room: 8, 9
 Finished creating Blank Rooms
 About to run CoR - createMap
 About to read from File :
 /storage/emulated/0/Android/data/com.IFaBb.ModularZombieSurvival/files/SaveFiles/Blarg.txt
 Not new File (skipping dims)
 Loop Spawn Rooms; 9, 10
 roomScript.roomConfig(DifferentRooms[0], ID:0, Rot:0, X:0, Z:0);
 Finished Configuring Room: 0, 0
 roomScript.roomConfig(DifferentRooms[0], ID:0, Rot:0, X:0, Z:1);
 ...
 Finished Configuring Room: 6, 0
 roomScript.roomConfig(DifferentRooms[0], ID:0, Rot:0, X:6, Z:1);
 Finished Configuring Room: 6, 1
 roomScript.roomConfig(DifferentRooms[0], ID:0, Rot:0, X:6, Z:2);

End of Output

So the problem here, is that all the Blank Root rooms are created, but the program stopped at Room 6,1 when giving each the appropriate room.

I either need advice on how to sort out the code concept below, which is to spawn all the roots, then the rooms, and then config them. AND/OR A way to do sequential CoR excutions, that each one is able to perform 2 for loops, and stop itself and then resume at the end of each, ie:

for(){ for(){

 //do stuff

} //jump out for now (yield) and resume next time CoR runs }

CODE (unimportant content removed)

 using UnityEngine;
 using System.Collections;
 
 using System.Collections.Generic;
 using System.IO;
 
 public class MapModeManager : MonoBehaviour {
     
     //Variables
     
     //==============================================================================================================
     /*Use this for initialization
      */
     void Start () {
 
         MyDebug.log("MapModeManager.start");
 
         StartCoroutine( coStart() );
 
     }//Start
 
     //==============================================================================================================
     IEnumerator coStart(){
     
         MyDebug.log("About to run CoR - createBlankMap");
         yield return StartCoroutine( createBlankMap() );
 
         MyDebug.log("About to run CoR - createMap");
         yield return StartCoroutine( createMap() );
 
         MyDebug.log("About to run CoR - activateNMs");
         yield return StartCoroutine( activateNMs() );
 
 
         if( !SaveMemory.isEditMode() ){
             MyDebug.log("Activating AI Manager");
             gameObject.transform.GetComponent<AI_Manager>().enabled = true;//enable AI script
             gameObject.transform.GetComponent<AI_Manager>().activate();//run startup stuff
         }else{
             MyDebug.log("Not, Activating AI Manager");
         }
 
         yield return null;
 
     }
 
 
     //==============================================================================================================
     IEnumerator createBlankMap(){
         
         for(int x=0; x<roomsX; x++)
         {
             for(int z=0; z<roomsZ; z++)
             {
                 MyDebug.log("Creating Blank Room: "+x+", "+z);
 
                 if( startingTemplateRoom == null ){
                     MyDebug.log("StartingTemplateRoom == Null");
                 }else{
                     Debug.Log("StartingTemplateRoom, Exists");
                 }
 
                 Rooms[x,z] = Instantiate( startingTemplateRoom,
                                          new Vector3( (float)x*spawnRoomSize,0f, (float)z*spawnRoomSize  ) ,
                                          Quaternion.identity ) as GameObject;
                 
             }//for
             
             yield return null;
             
         }//for
 
         MyDebug.log("Finished creating Blank Rooms");
 
         yield return null;
 
     }//IE createBlankMap
 
 
     //==============================================================================================================
     IEnumerator createMap(){
 
         //set Room Size
         Room roomScript;
         int roomID, roomRot;
         
         MyDebug.log("About to read from File :");
         MyDebug.log(SaveMemory.getDesiredFile(true) );
         
         using( StreamReader sr = new StreamReader( SaveMemory.getDesiredFile(true) ) ){
             
             
             MyDebug.log("Loop Spawn Rooms; "+roomsX+", "+roomsZ);
             //initializes Rooms
             for(int x=0; x<roomsX; x++){
                 for(int z=0; z<roomsZ; z++){
                     //------
                     
                     roomScript = Rooms[x,z].GetComponent<Room>();
                     
                     if( !SaveMemory.isNewFile() ){
                         string Temp = (sr.ReadLine()).ToString();
                         //Debug.Log("First Reading as String is :"+Temp);
                         roomID = int.Parse( Temp );
                         Temp = (sr.ReadLine()).ToString();
                         //Debug.Log("First Reading as String is :"+Temp);
                         roomRot= int.Parse( Temp );
                         
                         MyDebug.log("roomScript.roomConfig(DifferentRooms["+roomID+"], ID:"+roomID+", Rot:"+roomRot+", X:"+x+", Z:"+z+");");
                         
                         roomScript.roomConfig(DifferentRooms[roomID],roomID,roomRot, x, z);
                         
                     }else{
                         MyDebug.log( "Creating Basic Room" );
                         roomScript.roomConfig(DifferentRooms[0],0,0, x, z);
                     }
 
                 }//for
 
                 yield return null;
 
                 //Debug.Log("Row :"+x);
             }//for
         }//using
 
         MyDebug.log("CoR createMap, Finished");
         finishedSpawning = true;
 
         yield return null;
 
     }//IE createMap
 
 
     //==============================================================================================================
     IEnumerator activateNMs(){
 
         bool activateFinished = false;
 
         for(int x=0; x<roomsX; x++)
         {
             for(int z=0; z<roomsZ; z++)
             {
                 activateFinished = Rooms[x,z].GetComponent<Room>().activateNM();
             }
 
             yield return null;
 
         }//for
 
 
         MyDebug.log("About to Spawn Player Model");
         if( editMode ){
             
             if( DesktopApplication ){
                 Instantiate( EditorPlayer,             spawnPlayerPos.transform.position, Quaternion.identity);
             }else
             {
                 Instantiate( EditorPlayerMobile,     spawnPlayerPos.transform.position, Quaternion.identity);
             }
         }else{
             if( DesktopApplication ){
                 Instantiate( CombatPlayer,             spawnPlayerPos.transform.position, Quaternion.identity);
             }else
             {
                 Instantiate( CombatPlayerMobile,     spawnPlayerPos.transform.position, Quaternion.identity);
             }
         }//editMode
         Destroy(tempCamera);
 
         MyDebug.log("Have spawned Player");
 
         yield return null;
         
     }//activateNMs
     
     
     //==============================================================================================================
     // Update is called once per frame
     void Update () {
 
 
     }//Update
     
 }//CLASS
 

CODE Ends

Any and all help is welcome, and I will answer any and all questions if there are any.

Comment
Add comment · Show 3
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 AlwaysSunny · Aug 28, 2014 at 12:46 AM 0
Share

Sadly for folks with complex questions, it's unlikely you'll get any assistance when it involves debugging ~200 lines of code.

Besides which, I only halfway understand the issue: A chain of related co-routines sometimes fails to complete execution, and you haven't been able to find the cause. Is this correct?

I don't suppose you require advice about debugging or a link to co-routine documentation, but that's all anyone is likely to offer when the problem scope is difficult to communicate.

Unless each stage of the process requires that sibling objects have undergone the previous stage, then nothing is gained by splitting the process into separate stages.

In other words, could you reduce complexity by perfor$$anonymous$$g all N stages on one object at a time, rather than Stage1 on N objects, Stage2 on N objects, etc? Neither approach is superior if they're interchangeable, but perhaps you'll spot your issue while refactoring.

All co-routine functionality is exposed, so it's not as though there are any secrets. Based on the description of your desired behavior, it's possible your approach could be reworked to reduce complexity while still leveraging the benefits of co-routines.

avatar image Yinoguns · Aug 29, 2014 at 01:39 PM 0
Share

Thanks Sunny, thing have been working strangely with an error sometimes not showing the next time.

I have this one error which is when I try to run a method on a referenced script I obtain through GetComponent, but for some reason it just doesn't exist.

avatar image illustir · Sep 23, 2015 at 02:57 PM 0
Share

I had a similar issue today where converting a bunch of nested code into Coroutines would create only half of it be executed and no error messages showing up. Really really weird.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Yinoguns · Aug 29, 2014 at 02:15 PM

Ok im going to put a closing Answer on this.

Firstly CoR are a bit odd at first I thought they were exiting early, but turns out I missed an error I had.

I have a method called "GetScripts" which as the name says, gets the PathNode and NodeManager scripts for the room.

I hadn't worked with all the code for sometime and had been working on other areas, I didnt realise that GetScripts was restricting its operation depending on gamemode.

From this I wont be forgetting or making the mistakes I had made when making the game, so no worries.

 public void getScripts(){
 
         if( !SaveMenory.isEditMode ){
 
             Debug.Log("Getting Scripts");
 
             if( theRoom.GetComponent<PathNode>() != null ){
                 PathingNode = theRoom.GetComponent<PathNode>();
                 //Debug.Log("Room.Grabbing PathNode");
             }
             if(PathingNode == null){
                 MyDebug.log("Room ["+roomX+","+roomZ+"] PathNode is still NULL!");
             }
 
             if( theRoom.GetComponent<NodeManager>() != null ){
                 NodeMan = theRoom.GetComponent<NodeManager>();
                 //Debug.Log("Room.Grabbing NodeManager");
             }
             if(NodeMan == null){
                 MyDebug.log("Room ["+roomX+","+roomZ+"] NodeMan is still NULL!");
             }
         
         }else{
 
             //Debug.Log("EditMode; not grabbing Scripts");
             BroadcastMessage("disablePlayModeAspects",SendMessageOptions.DontRequireReceiver);
         
         }
         
     }//getScripts

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Make a GameObject changes between 2 colors with a time wait 1 Answer

Loops without coroutine [SOLVED] 3 Answers

How to make loop with call to IEnumerator actually pause? 2 Answers

Coroutine not running 1 Answer

Can I use WWW in Start()? 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