Search Unity

Problem with Coroutine on Run Time

Discussion in 'Scripting' started by alone1992, May 13, 2015.

  1. alone1992

    alone1992

    Joined:
    Oct 23, 2012
    Posts:
    156
    Hello Every one
    I have a simple Coroutine that have problem when load scene from game , in other words when I use unity3d Editor play button to test this code every thing is right but when I try to test it by running this code scene from level load every thing is stop. 1=>2 (on level start ok) 2=>1 & 1=>2(error)
    I have game that every thing is almost finished but with this problem when user for 2nd time wants to go to main level counter freezes!!


    Code (JavaScript):
    1.  
    2.         function Start () {
    3.      
    4.         StartCoroutine("DoSomething");
    5.  
    6.     }
    7.  
    8. function DoSomething () {
    9.  
    10.     for (var i = 5; i >= 0; --i) {
    11.          
    12.             print("Future : \n" + i);
    13.     yield WaitForSeconds(1);
    14.             print("counting : \n " + i);
    15. }
    16.      
    17.     }
    thanks in advance
     
  2. alone1992

    alone1992

    Joined:
    Oct 23, 2012
    Posts:
    156
    You can test it by having 2 scenes and with a button move from scene1 to scene2 and from scene2 to scene1 and Add this code to an object in second scene.
     
    Last edited: May 14, 2015
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I've put together a test project as you said. The code works fine, scene1=>scene2=>scene1=>scene1=>scene2=>scene2=>scene2=>scene1=>scene2 .. etc.

    always when scene 2 starts where the above code is on an empty gameobject the debug lines count through. Your problem appears to be elsewhere in your code/project.

    What "error" do you get? or were you referring to it not working?
     
    alone1992 likes this.
  4. alone1992

    alone1992

    Joined:
    Oct 23, 2012
    Posts:
    156
    at first time its work properly but at the second loading time its freezes on 12th line and just print that.
    In any other example with yield wait for second or ... with unity3d 5.0.0f1 this damned error happens , In my unity3d 4.6 version every thing works very well:rolleyes: but after upgrading this problem occurred
    I think I should test it on empty one maybe its from other place :confused:
    Is there any way to stop this Coroutine or reset it? I think my Coroutine after first start do not stop and in second time its hang on.
    thanks
     
    Last edited: May 15, 2015
  5. alone1992

    alone1992

    Joined:
    Oct 23, 2012
    Posts:
    156
    I think main problem will be on upgrading from unity 4.6 to unity 5 because before updating it's work very well.
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I was using 5.0.0f1, but it was a clean new project, if you've converted a project from an older version to the newer version I guess it's possible it's messed something up. Hard to check that here though :confused:
     
    alone1992 likes this.
  7. alone1992

    alone1992

    Joined:
    Oct 23, 2012
    Posts:
    156
    I find the problem :D
    It's on my this code :
    Code (JavaScript):
    1.     if(BoolBack){
    2.         if(IsPaused){
    3.             Loading.transform.position = Vector3(-0.335f ,0.979f ,-10.861f);
    4.             Application.LoadLevel("menu");
    5.             BoolBack = false;
    6.         }
    7.         else{
    8.             Loading.transform.position = Vector3(1.32f ,0.979f ,-10.861f);
    9.             Pausing();
    10.             BoolBack = false;
    11.         }      
    12. function Pausing () {
    13.  
    14.     Time.timeScale = 0.00000001f;
    15.     Paused.transform.position = Vector3(-0.3344844f ,0.9809325f ,-9.741899f);
    16.     IsPaused = true;  
    17.     BoolPause = false;
    18.     BoolBack = false;
    19.     return;
    20. }
    21.  

    its say before going to main menu scene game paused and when come back to game scene its still paused
    I think in unity3d 4.6 its reset by default but in new version unity3d 5.0 its saved like a static variable.
    I find this issue with :

    Code (JavaScript):
    1.     Debug.Log("Time Scale is:" + Time.timeScale);
    2.  
    thanks
     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148