Unity3D Raycasting in Start behaves uncorrectly, but works correctly in Update? The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Map logical coordinates to graphical for walkingUsing EaselJS, my spritesheet-based map is not showing upHow can I make my tiles fall smoothly?Raycast will not hit generated box colliders at initializationMissing Reference: Unity should clone object before destroyingUnity 2D sprite animation keep in sync after setting inactiveUnity MonoBehaviour seems to have another scopeRaycast in Unity for ground detection returns false while touching groundHelp Improving Map GenerationStoring 2d map data as PNG

Python - Fishing Simulator

Netflix Recommendations?

Difference between "generating set" and free product?

The variadic template constructor of my class cannot modify my class members, why is that so?

ELI5: Why do they say that Israel would have been the fourth country to land a spacecraft on the Moon and why do they call it low cost?

Working through the single responsibility principle (SRP) in Python when calls are expensive

Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?

What do you call a plan that's an alternative plan in case your initial plan fails?

The following signatures were invalid: EXPKEYSIG 1397BC53640DB551

Can smartphones with the same camera sensor have different image quality?

how can a perfect fourth interval be considered either consonant or dissonant?

Make it rain characters

What aspect of planet Earth must be changed to prevent the industrial revolution?

How to test the equality of two Pearson correlation coefficients computed from the same sample?

Am I ethically obligated to go into work on an off day if the reason is sudden?

Can undead you have reanimated wait inside a portable hole?

Why can't wing-mounted spoilers be used to steepen approaches?

Can withdrawing asylum be illegal?

Road tyres vs "Street" tyres for charity ride on MTB Tandem

Do warforged have souls?

Single author papers against my advisor's will?

How are presidential pardons supposed to be used?

University's motivation for having tenure-track positions

Is there a writing software that you can sort scenes like slides in PowerPoint?



Unity3D Raycasting in Start behaves uncorrectly, but works correctly in Update?



The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Map logical coordinates to graphical for walkingUsing EaselJS, my spritesheet-based map is not showing upHow can I make my tiles fall smoothly?Raycast will not hit generated box colliders at initializationMissing Reference: Unity should clone object before destroyingUnity 2D sprite animation keep in sync after setting inactiveUnity MonoBehaviour seems to have another scopeRaycast in Unity for ground detection returns false while touching groundHelp Improving Map GenerationStoring 2d map data as PNG



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1












$begingroup$


I have this method, which just raycasts at a position downwards, and checks what tile is there, if there is one:



 public static Tile RaycastTile(Vector3 position)

Debug.Log("raycastTile at pos: " + position);
var layerMask = 1 << LayerMask.NameToLayer("Floor");
RaycastHit hit;
Debug.DrawRay(position, new Vector3(0, -10, 0), Color.red, 50);
if (Physics.Raycast(position, new Vector3(0, -1, 0), out hit, 10, layerMask))

var tile = hit.collider.gameObject.transform;
Debug.LogWarning(tile.name + " - " + tile.parent.name + " - at pos: " + tile.position);
var coordinates = new Vector2Int(Mathf.RoundToInt(tile.position.x) / 2, Mathf.RoundToInt(tile.position.z) / 2);
return map.GetTile(coordinates);

else

Debug.LogError("Tile not found under position: " + position);
Debug.Break();
return null;




Currently it is called for the player



  • at Start() ("starting at: < player's absolute position >")


  • and at Update() ("update at pos: < player's absolute position >")


  • And the map is being generated (tiles created, destroyed, etc) in Awake().


and for some reason this is the output:



enter image description here



And the map generation works like this:



  • Creates a room at (0, 0) with tiles and all, then it searches for a connection point on the existing map, whom the new room could be connected to. (with some rotation, if it's needed)


  • (Every tile is 2x2 meters, and they are on a strict grid)


So for some reason calling this method at Start() (with the same position) hits a totally different tile. While in Update() it works correctly.



At first I thought that it's because of Destroy(), because the objects are only destroyed at the end of the frame. But I tried DestroyImmediate() and it didn't help. (And after thinking, I realised that destroying objects at the end of the frame wouldn't cause issues. Or is it?)



Then I had the idea, that maybe Start doesn't wait for Awake because it compute heavy. And the raycast in the Start hits a (yet) non translated newly generated room. So I edited the code so every room is initially created at very far away, which is relatively "infinite far away" from the player: at (9999, 9999). But the issue remained.



EDIT: With (9999, 9999) Sometimes (maybe half of the time?) now it is correctly raycasted even at start, while before it was never correct in start.



EDIT2: With (0, 0) it hits correctly at start as well, but very very rarely.



Do you have any idea what could cause this issue?



Sorry if there isn't enough info, feel free to ask. :)










share|improve this question











$endgroup$


















    1












    $begingroup$


    I have this method, which just raycasts at a position downwards, and checks what tile is there, if there is one:



     public static Tile RaycastTile(Vector3 position)

    Debug.Log("raycastTile at pos: " + position);
    var layerMask = 1 << LayerMask.NameToLayer("Floor");
    RaycastHit hit;
    Debug.DrawRay(position, new Vector3(0, -10, 0), Color.red, 50);
    if (Physics.Raycast(position, new Vector3(0, -1, 0), out hit, 10, layerMask))

    var tile = hit.collider.gameObject.transform;
    Debug.LogWarning(tile.name + " - " + tile.parent.name + " - at pos: " + tile.position);
    var coordinates = new Vector2Int(Mathf.RoundToInt(tile.position.x) / 2, Mathf.RoundToInt(tile.position.z) / 2);
    return map.GetTile(coordinates);

    else

    Debug.LogError("Tile not found under position: " + position);
    Debug.Break();
    return null;




    Currently it is called for the player



    • at Start() ("starting at: < player's absolute position >")


    • and at Update() ("update at pos: < player's absolute position >")


    • And the map is being generated (tiles created, destroyed, etc) in Awake().


    and for some reason this is the output:



    enter image description here



    And the map generation works like this:



    • Creates a room at (0, 0) with tiles and all, then it searches for a connection point on the existing map, whom the new room could be connected to. (with some rotation, if it's needed)


    • (Every tile is 2x2 meters, and they are on a strict grid)


    So for some reason calling this method at Start() (with the same position) hits a totally different tile. While in Update() it works correctly.



    At first I thought that it's because of Destroy(), because the objects are only destroyed at the end of the frame. But I tried DestroyImmediate() and it didn't help. (And after thinking, I realised that destroying objects at the end of the frame wouldn't cause issues. Or is it?)



    Then I had the idea, that maybe Start doesn't wait for Awake because it compute heavy. And the raycast in the Start hits a (yet) non translated newly generated room. So I edited the code so every room is initially created at very far away, which is relatively "infinite far away" from the player: at (9999, 9999). But the issue remained.



    EDIT: With (9999, 9999) Sometimes (maybe half of the time?) now it is correctly raycasted even at start, while before it was never correct in start.



    EDIT2: With (0, 0) it hits correctly at start as well, but very very rarely.



    Do you have any idea what could cause this issue?



    Sorry if there isn't enough info, feel free to ask. :)










    share|improve this question











    $endgroup$














      1












      1








      1





      $begingroup$


      I have this method, which just raycasts at a position downwards, and checks what tile is there, if there is one:



       public static Tile RaycastTile(Vector3 position)

      Debug.Log("raycastTile at pos: " + position);
      var layerMask = 1 << LayerMask.NameToLayer("Floor");
      RaycastHit hit;
      Debug.DrawRay(position, new Vector3(0, -10, 0), Color.red, 50);
      if (Physics.Raycast(position, new Vector3(0, -1, 0), out hit, 10, layerMask))

      var tile = hit.collider.gameObject.transform;
      Debug.LogWarning(tile.name + " - " + tile.parent.name + " - at pos: " + tile.position);
      var coordinates = new Vector2Int(Mathf.RoundToInt(tile.position.x) / 2, Mathf.RoundToInt(tile.position.z) / 2);
      return map.GetTile(coordinates);

      else

      Debug.LogError("Tile not found under position: " + position);
      Debug.Break();
      return null;




      Currently it is called for the player



      • at Start() ("starting at: < player's absolute position >")


      • and at Update() ("update at pos: < player's absolute position >")


      • And the map is being generated (tiles created, destroyed, etc) in Awake().


      and for some reason this is the output:



      enter image description here



      And the map generation works like this:



      • Creates a room at (0, 0) with tiles and all, then it searches for a connection point on the existing map, whom the new room could be connected to. (with some rotation, if it's needed)


      • (Every tile is 2x2 meters, and they are on a strict grid)


      So for some reason calling this method at Start() (with the same position) hits a totally different tile. While in Update() it works correctly.



      At first I thought that it's because of Destroy(), because the objects are only destroyed at the end of the frame. But I tried DestroyImmediate() and it didn't help. (And after thinking, I realised that destroying objects at the end of the frame wouldn't cause issues. Or is it?)



      Then I had the idea, that maybe Start doesn't wait for Awake because it compute heavy. And the raycast in the Start hits a (yet) non translated newly generated room. So I edited the code so every room is initially created at very far away, which is relatively "infinite far away" from the player: at (9999, 9999). But the issue remained.



      EDIT: With (9999, 9999) Sometimes (maybe half of the time?) now it is correctly raycasted even at start, while before it was never correct in start.



      EDIT2: With (0, 0) it hits correctly at start as well, but very very rarely.



      Do you have any idea what could cause this issue?



      Sorry if there isn't enough info, feel free to ask. :)










      share|improve this question











      $endgroup$




      I have this method, which just raycasts at a position downwards, and checks what tile is there, if there is one:



       public static Tile RaycastTile(Vector3 position)

      Debug.Log("raycastTile at pos: " + position);
      var layerMask = 1 << LayerMask.NameToLayer("Floor");
      RaycastHit hit;
      Debug.DrawRay(position, new Vector3(0, -10, 0), Color.red, 50);
      if (Physics.Raycast(position, new Vector3(0, -1, 0), out hit, 10, layerMask))

      var tile = hit.collider.gameObject.transform;
      Debug.LogWarning(tile.name + " - " + tile.parent.name + " - at pos: " + tile.position);
      var coordinates = new Vector2Int(Mathf.RoundToInt(tile.position.x) / 2, Mathf.RoundToInt(tile.position.z) / 2);
      return map.GetTile(coordinates);

      else

      Debug.LogError("Tile not found under position: " + position);
      Debug.Break();
      return null;




      Currently it is called for the player



      • at Start() ("starting at: < player's absolute position >")


      • and at Update() ("update at pos: < player's absolute position >")


      • And the map is being generated (tiles created, destroyed, etc) in Awake().


      and for some reason this is the output:



      enter image description here



      And the map generation works like this:



      • Creates a room at (0, 0) with tiles and all, then it searches for a connection point on the existing map, whom the new room could be connected to. (with some rotation, if it's needed)


      • (Every tile is 2x2 meters, and they are on a strict grid)


      So for some reason calling this method at Start() (with the same position) hits a totally different tile. While in Update() it works correctly.



      At first I thought that it's because of Destroy(), because the objects are only destroyed at the end of the frame. But I tried DestroyImmediate() and it didn't help. (And after thinking, I realised that destroying objects at the end of the frame wouldn't cause issues. Or is it?)



      Then I had the idea, that maybe Start doesn't wait for Awake because it compute heavy. And the raycast in the Start hits a (yet) non translated newly generated room. So I edited the code so every room is initially created at very far away, which is relatively "infinite far away" from the player: at (9999, 9999). But the issue remained.



      EDIT: With (9999, 9999) Sometimes (maybe half of the time?) now it is correctly raycasted even at start, while before it was never correct in start.



      EDIT2: With (0, 0) it hits correctly at start as well, but very very rarely.



      Do you have any idea what could cause this issue?



      Sorry if there isn't enough info, feel free to ask. :)







      unity tilemap raycasting






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 31 at 10:14







      Tudvari

















      asked Mar 31 at 9:54









      TudvariTudvari

      214316




      214316




















          1 Answer
          1






          active

          oldest

          votes


















          2












          $begingroup$

          The game world of Awake/Start/Update, and the physics world of PhysX, are actually two separate sets of data. It takes some extra work from the engine to apply changes from one world to the other.



          Normally this is done once in the physics step for each FixedUpdate, but depending on frame timings, we might not get a physics step / FixedUpdate in the frame that Start runs.



          My best guess here is that Start is sometimes asking the physics engine to raycast before the physics engine has been told about all of the tiles, so it gets stale data.



          According to the docs, you can force a sync manually with Physics.SyncTransforms, which might help flush any pending transform updates through the pipe before you start your raycast.



          Or, if your game can tolerate taking a moment to settle when the objects are first spawned, you can let Start wait for a FixedUpdate or two for the physics to be up to date before it tries to raycast against it, something like this:



          IEnumerator Start() 
          // Wait until the first physics step runs...
          yield return new WaitForFixedUpdate();
          // Wait until later in that frame,
          // after physics is stepped
          yield return null;

          // Now do physics tests.







          share|improve this answer









          $endgroup$












          • $begingroup$
            Physics.SyncTransforms doesn't seem to fix it, I think it's async. But waiting for the first FixedUpdate seems fine. How does that "yield return null" works? And do you know a way to delay every start? Because starting a coroutine in every MonoBehaviour seems pretty dull, it would be better to just systematically wait one fixed update between Awake and Start.
            $endgroup$
            – Tudvari
            Mar 31 at 16:29










          • $begingroup$
            And is it sure that my coroutine will finish by the time of my first update? Update: I just have 2 players, and the order was this: Coroutine1, Update1, Coroutine2 This could cause some undeterministic issues :
            $endgroup$
            – Tudvari
            Mar 31 at 16:34






          • 1




            $begingroup$
            To delay all of your setup scripts without coordinating tons of coroutines, I'd recommend giving them an interface like IInitializable, and implement their setup logic in public Initialize methods. Then your level generator can do its generation, start a coroutine, then call all the Initialize() methods in a batch once the physics have ticked once to ready the level. That way you can be sure all of the initialization happens at the same time. For deeper information on Coroutine timing, I recommend reading the docs on execution order
            $endgroup$
            – DMGregory
            Mar 31 at 16:39










          • $begingroup$
            Having nearly everything descended from IInitializable is a bit cumbersome, isn't it? Isn't there a way to separate initialization of the scene and starting of the scene? For example the scene won't call it's Awake, Start, ... methods until I signal OK.
            $endgroup$
            – Tudvari
            Apr 2 at 22:15






          • 1




            $begingroup$
            There is no such requirement to descend from a particular class. If you have questions about implementing this, feel free to post a new Question.
            $endgroup$
            – DMGregory
            Apr 2 at 23:04











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "53"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgamedev.stackexchange.com%2fquestions%2f169536%2funity3d-raycasting-in-start-behaves-uncorrectly-but-works-correctly-in-update%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2












          $begingroup$

          The game world of Awake/Start/Update, and the physics world of PhysX, are actually two separate sets of data. It takes some extra work from the engine to apply changes from one world to the other.



          Normally this is done once in the physics step for each FixedUpdate, but depending on frame timings, we might not get a physics step / FixedUpdate in the frame that Start runs.



          My best guess here is that Start is sometimes asking the physics engine to raycast before the physics engine has been told about all of the tiles, so it gets stale data.



          According to the docs, you can force a sync manually with Physics.SyncTransforms, which might help flush any pending transform updates through the pipe before you start your raycast.



          Or, if your game can tolerate taking a moment to settle when the objects are first spawned, you can let Start wait for a FixedUpdate or two for the physics to be up to date before it tries to raycast against it, something like this:



          IEnumerator Start() 
          // Wait until the first physics step runs...
          yield return new WaitForFixedUpdate();
          // Wait until later in that frame,
          // after physics is stepped
          yield return null;

          // Now do physics tests.







          share|improve this answer









          $endgroup$












          • $begingroup$
            Physics.SyncTransforms doesn't seem to fix it, I think it's async. But waiting for the first FixedUpdate seems fine. How does that "yield return null" works? And do you know a way to delay every start? Because starting a coroutine in every MonoBehaviour seems pretty dull, it would be better to just systematically wait one fixed update between Awake and Start.
            $endgroup$
            – Tudvari
            Mar 31 at 16:29










          • $begingroup$
            And is it sure that my coroutine will finish by the time of my first update? Update: I just have 2 players, and the order was this: Coroutine1, Update1, Coroutine2 This could cause some undeterministic issues :
            $endgroup$
            – Tudvari
            Mar 31 at 16:34






          • 1




            $begingroup$
            To delay all of your setup scripts without coordinating tons of coroutines, I'd recommend giving them an interface like IInitializable, and implement their setup logic in public Initialize methods. Then your level generator can do its generation, start a coroutine, then call all the Initialize() methods in a batch once the physics have ticked once to ready the level. That way you can be sure all of the initialization happens at the same time. For deeper information on Coroutine timing, I recommend reading the docs on execution order
            $endgroup$
            – DMGregory
            Mar 31 at 16:39










          • $begingroup$
            Having nearly everything descended from IInitializable is a bit cumbersome, isn't it? Isn't there a way to separate initialization of the scene and starting of the scene? For example the scene won't call it's Awake, Start, ... methods until I signal OK.
            $endgroup$
            – Tudvari
            Apr 2 at 22:15






          • 1




            $begingroup$
            There is no such requirement to descend from a particular class. If you have questions about implementing this, feel free to post a new Question.
            $endgroup$
            – DMGregory
            Apr 2 at 23:04















          2












          $begingroup$

          The game world of Awake/Start/Update, and the physics world of PhysX, are actually two separate sets of data. It takes some extra work from the engine to apply changes from one world to the other.



          Normally this is done once in the physics step for each FixedUpdate, but depending on frame timings, we might not get a physics step / FixedUpdate in the frame that Start runs.



          My best guess here is that Start is sometimes asking the physics engine to raycast before the physics engine has been told about all of the tiles, so it gets stale data.



          According to the docs, you can force a sync manually with Physics.SyncTransforms, which might help flush any pending transform updates through the pipe before you start your raycast.



          Or, if your game can tolerate taking a moment to settle when the objects are first spawned, you can let Start wait for a FixedUpdate or two for the physics to be up to date before it tries to raycast against it, something like this:



          IEnumerator Start() 
          // Wait until the first physics step runs...
          yield return new WaitForFixedUpdate();
          // Wait until later in that frame,
          // after physics is stepped
          yield return null;

          // Now do physics tests.







          share|improve this answer









          $endgroup$












          • $begingroup$
            Physics.SyncTransforms doesn't seem to fix it, I think it's async. But waiting for the first FixedUpdate seems fine. How does that "yield return null" works? And do you know a way to delay every start? Because starting a coroutine in every MonoBehaviour seems pretty dull, it would be better to just systematically wait one fixed update between Awake and Start.
            $endgroup$
            – Tudvari
            Mar 31 at 16:29










          • $begingroup$
            And is it sure that my coroutine will finish by the time of my first update? Update: I just have 2 players, and the order was this: Coroutine1, Update1, Coroutine2 This could cause some undeterministic issues :
            $endgroup$
            – Tudvari
            Mar 31 at 16:34






          • 1




            $begingroup$
            To delay all of your setup scripts without coordinating tons of coroutines, I'd recommend giving them an interface like IInitializable, and implement their setup logic in public Initialize methods. Then your level generator can do its generation, start a coroutine, then call all the Initialize() methods in a batch once the physics have ticked once to ready the level. That way you can be sure all of the initialization happens at the same time. For deeper information on Coroutine timing, I recommend reading the docs on execution order
            $endgroup$
            – DMGregory
            Mar 31 at 16:39










          • $begingroup$
            Having nearly everything descended from IInitializable is a bit cumbersome, isn't it? Isn't there a way to separate initialization of the scene and starting of the scene? For example the scene won't call it's Awake, Start, ... methods until I signal OK.
            $endgroup$
            – Tudvari
            Apr 2 at 22:15






          • 1




            $begingroup$
            There is no such requirement to descend from a particular class. If you have questions about implementing this, feel free to post a new Question.
            $endgroup$
            – DMGregory
            Apr 2 at 23:04













          2












          2








          2





          $begingroup$

          The game world of Awake/Start/Update, and the physics world of PhysX, are actually two separate sets of data. It takes some extra work from the engine to apply changes from one world to the other.



          Normally this is done once in the physics step for each FixedUpdate, but depending on frame timings, we might not get a physics step / FixedUpdate in the frame that Start runs.



          My best guess here is that Start is sometimes asking the physics engine to raycast before the physics engine has been told about all of the tiles, so it gets stale data.



          According to the docs, you can force a sync manually with Physics.SyncTransforms, which might help flush any pending transform updates through the pipe before you start your raycast.



          Or, if your game can tolerate taking a moment to settle when the objects are first spawned, you can let Start wait for a FixedUpdate or two for the physics to be up to date before it tries to raycast against it, something like this:



          IEnumerator Start() 
          // Wait until the first physics step runs...
          yield return new WaitForFixedUpdate();
          // Wait until later in that frame,
          // after physics is stepped
          yield return null;

          // Now do physics tests.







          share|improve this answer









          $endgroup$



          The game world of Awake/Start/Update, and the physics world of PhysX, are actually two separate sets of data. It takes some extra work from the engine to apply changes from one world to the other.



          Normally this is done once in the physics step for each FixedUpdate, but depending on frame timings, we might not get a physics step / FixedUpdate in the frame that Start runs.



          My best guess here is that Start is sometimes asking the physics engine to raycast before the physics engine has been told about all of the tiles, so it gets stale data.



          According to the docs, you can force a sync manually with Physics.SyncTransforms, which might help flush any pending transform updates through the pipe before you start your raycast.



          Or, if your game can tolerate taking a moment to settle when the objects are first spawned, you can let Start wait for a FixedUpdate or two for the physics to be up to date before it tries to raycast against it, something like this:



          IEnumerator Start() 
          // Wait until the first physics step runs...
          yield return new WaitForFixedUpdate();
          // Wait until later in that frame,
          // after physics is stepped
          yield return null;

          // Now do physics tests.








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 31 at 11:12









          DMGregoryDMGregory

          64.9k16115180




          64.9k16115180











          • $begingroup$
            Physics.SyncTransforms doesn't seem to fix it, I think it's async. But waiting for the first FixedUpdate seems fine. How does that "yield return null" works? And do you know a way to delay every start? Because starting a coroutine in every MonoBehaviour seems pretty dull, it would be better to just systematically wait one fixed update between Awake and Start.
            $endgroup$
            – Tudvari
            Mar 31 at 16:29










          • $begingroup$
            And is it sure that my coroutine will finish by the time of my first update? Update: I just have 2 players, and the order was this: Coroutine1, Update1, Coroutine2 This could cause some undeterministic issues :
            $endgroup$
            – Tudvari
            Mar 31 at 16:34






          • 1




            $begingroup$
            To delay all of your setup scripts without coordinating tons of coroutines, I'd recommend giving them an interface like IInitializable, and implement their setup logic in public Initialize methods. Then your level generator can do its generation, start a coroutine, then call all the Initialize() methods in a batch once the physics have ticked once to ready the level. That way you can be sure all of the initialization happens at the same time. For deeper information on Coroutine timing, I recommend reading the docs on execution order
            $endgroup$
            – DMGregory
            Mar 31 at 16:39










          • $begingroup$
            Having nearly everything descended from IInitializable is a bit cumbersome, isn't it? Isn't there a way to separate initialization of the scene and starting of the scene? For example the scene won't call it's Awake, Start, ... methods until I signal OK.
            $endgroup$
            – Tudvari
            Apr 2 at 22:15






          • 1




            $begingroup$
            There is no such requirement to descend from a particular class. If you have questions about implementing this, feel free to post a new Question.
            $endgroup$
            – DMGregory
            Apr 2 at 23:04
















          • $begingroup$
            Physics.SyncTransforms doesn't seem to fix it, I think it's async. But waiting for the first FixedUpdate seems fine. How does that "yield return null" works? And do you know a way to delay every start? Because starting a coroutine in every MonoBehaviour seems pretty dull, it would be better to just systematically wait one fixed update between Awake and Start.
            $endgroup$
            – Tudvari
            Mar 31 at 16:29










          • $begingroup$
            And is it sure that my coroutine will finish by the time of my first update? Update: I just have 2 players, and the order was this: Coroutine1, Update1, Coroutine2 This could cause some undeterministic issues :
            $endgroup$
            – Tudvari
            Mar 31 at 16:34






          • 1




            $begingroup$
            To delay all of your setup scripts without coordinating tons of coroutines, I'd recommend giving them an interface like IInitializable, and implement their setup logic in public Initialize methods. Then your level generator can do its generation, start a coroutine, then call all the Initialize() methods in a batch once the physics have ticked once to ready the level. That way you can be sure all of the initialization happens at the same time. For deeper information on Coroutine timing, I recommend reading the docs on execution order
            $endgroup$
            – DMGregory
            Mar 31 at 16:39










          • $begingroup$
            Having nearly everything descended from IInitializable is a bit cumbersome, isn't it? Isn't there a way to separate initialization of the scene and starting of the scene? For example the scene won't call it's Awake, Start, ... methods until I signal OK.
            $endgroup$
            – Tudvari
            Apr 2 at 22:15






          • 1




            $begingroup$
            There is no such requirement to descend from a particular class. If you have questions about implementing this, feel free to post a new Question.
            $endgroup$
            – DMGregory
            Apr 2 at 23:04















          $begingroup$
          Physics.SyncTransforms doesn't seem to fix it, I think it's async. But waiting for the first FixedUpdate seems fine. How does that "yield return null" works? And do you know a way to delay every start? Because starting a coroutine in every MonoBehaviour seems pretty dull, it would be better to just systematically wait one fixed update between Awake and Start.
          $endgroup$
          – Tudvari
          Mar 31 at 16:29




          $begingroup$
          Physics.SyncTransforms doesn't seem to fix it, I think it's async. But waiting for the first FixedUpdate seems fine. How does that "yield return null" works? And do you know a way to delay every start? Because starting a coroutine in every MonoBehaviour seems pretty dull, it would be better to just systematically wait one fixed update between Awake and Start.
          $endgroup$
          – Tudvari
          Mar 31 at 16:29












          $begingroup$
          And is it sure that my coroutine will finish by the time of my first update? Update: I just have 2 players, and the order was this: Coroutine1, Update1, Coroutine2 This could cause some undeterministic issues :
          $endgroup$
          – Tudvari
          Mar 31 at 16:34




          $begingroup$
          And is it sure that my coroutine will finish by the time of my first update? Update: I just have 2 players, and the order was this: Coroutine1, Update1, Coroutine2 This could cause some undeterministic issues :
          $endgroup$
          – Tudvari
          Mar 31 at 16:34




          1




          1




          $begingroup$
          To delay all of your setup scripts without coordinating tons of coroutines, I'd recommend giving them an interface like IInitializable, and implement their setup logic in public Initialize methods. Then your level generator can do its generation, start a coroutine, then call all the Initialize() methods in a batch once the physics have ticked once to ready the level. That way you can be sure all of the initialization happens at the same time. For deeper information on Coroutine timing, I recommend reading the docs on execution order
          $endgroup$
          – DMGregory
          Mar 31 at 16:39




          $begingroup$
          To delay all of your setup scripts without coordinating tons of coroutines, I'd recommend giving them an interface like IInitializable, and implement their setup logic in public Initialize methods. Then your level generator can do its generation, start a coroutine, then call all the Initialize() methods in a batch once the physics have ticked once to ready the level. That way you can be sure all of the initialization happens at the same time. For deeper information on Coroutine timing, I recommend reading the docs on execution order
          $endgroup$
          – DMGregory
          Mar 31 at 16:39












          $begingroup$
          Having nearly everything descended from IInitializable is a bit cumbersome, isn't it? Isn't there a way to separate initialization of the scene and starting of the scene? For example the scene won't call it's Awake, Start, ... methods until I signal OK.
          $endgroup$
          – Tudvari
          Apr 2 at 22:15




          $begingroup$
          Having nearly everything descended from IInitializable is a bit cumbersome, isn't it? Isn't there a way to separate initialization of the scene and starting of the scene? For example the scene won't call it's Awake, Start, ... methods until I signal OK.
          $endgroup$
          – Tudvari
          Apr 2 at 22:15




          1




          1




          $begingroup$
          There is no such requirement to descend from a particular class. If you have questions about implementing this, feel free to post a new Question.
          $endgroup$
          – DMGregory
          Apr 2 at 23:04




          $begingroup$
          There is no such requirement to descend from a particular class. If you have questions about implementing this, feel free to post a new Question.
          $endgroup$
          – DMGregory
          Apr 2 at 23:04

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Game Development Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgamedev.stackexchange.com%2fquestions%2f169536%2funity3d-raycasting-in-start-behaves-uncorrectly-but-works-correctly-in-update%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Triangular numbers and gcdProving sum of a set is $0 pmod n$ if $n$ is odd, or $fracn2 pmod n$ if $n$ is even?Is greatest common divisor of two numbers really their smallest linear combination?GCD, LCM RelationshipProve a set of nonnegative integers with greatest common divisor 1 and closed under addition has all but finite many nonnegative integers.all pairs of a and b in an equation containing gcdTriangular Numbers Modulo $k$ - Hit All Values?Understanding the Existence and Uniqueness of the GCDGCD and LCM with logical symbolsThe greatest common divisor of two positive integers less than 100 is equal to 3. Their least common multiple is twelve times one of the integers.Suppose that for all integers $x$, $x|a$ and $x|b$ if and only if $x|c$. Then $c = gcd(a,b)$Which is the gcd of 2 numbers which are multiplied and the result is 600000?

          Ingelân Ynhâld Etymology | Geografy | Skiednis | Polityk en bestjoer | Ekonomy | Demografy | Kultuer | Klimaat | Sjoch ek | Keppelings om utens | Boarnen, noaten en referinsjes Navigaasjemenuwww.gov.ukOffisjele webside fan it regear fan it Feriene KeninkrykOffisjele webside fan it Britske FerkearsburoNederlânsktalige ynformaasje fan it Britske FerkearsburoOffisjele webside fan English Heritage, de organisaasje dy't him ynset foar it behâld fan it Ingelske kultuergoedYnwennertallen fan alle Britske stêden út 'e folkstelling fan 2011Notes en References, op dizze sideEngland

          Հադիս Բովանդակություն Անվանում և նշանակություն | Դասակարգում | Աղբյուրներ | Նավարկման ցանկ