How can I generate the units at the same time?












0















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

public class GenerateStairs : MonoBehaviour
{
[Header("Stairs Prefb")]
public GameObject stairsPrefab;
[Space(5)]
[Header("Platforms")]
public bool addPlatforms = false;
public GameObject platformsPrefab;
[Space(5)]
[Header("Settings")]
[Range(1, 20)]
public int numberOfUnits = 1;
public float delay = 3;
public int stairsNumber = 5;
public Vector3 stairsStartPosition;
public Vector3 stairSize;
public Vector3 stairsSize;
public float stepWidthFactor = 1f;

private Vector3 stairsPosition;
private GameObject stairsParent;
private int oldNumberOfUnits = 0;

// Use this for initialization
void Start()
{
oldNumberOfUnits = numberOfUnits;

for (int i = 0; i < numberOfUnits; i++)
{
stairsParent = new GameObject();
stairsParent.name = "Stairs";
StartCoroutine(BuildStairs());
}
}

// Update is called once per frame
void Update()
{
if(oldNumberOfUnits != numberOfUnits)
{
StartCoroutine(BuildStairs());
oldNumberOfUnits = numberOfUnits;
}
}

private IEnumerator BuildStairs()
{
for (int i = 1; i <= stairsNumber; i++)
{

stairsPosition = new Vector3(
stairsStartPosition.x,
stairsStartPosition.y + (i * stairsSize.y),
stairsStartPosition.z + (i * stairsSize.y) * stepWidthFactor);

GameObject stair = Instantiate(
stairsPrefab,
stairsPosition,
Quaternion.identity);

stair.tag = "Stair";
stair.transform.parent = stairsParent.transform;
stair.transform.localScale = stairSize;

yield return new WaitForSeconds(delay);
}

stairsParent.AddComponent<MoveObjects>().Init();
}
}


In the Start I'm doing a loop and start the Coroutine according to the numberOfunits.



It's working fine if numberOfUnits is 1. But is it's more then 1 for example 2 it's first creating the first set of stairs but then on the second "Stairs" parent it's creating only 1 stair. I don't want it to wait to finish the first Coroutine I want in the same time to create number of Coroutine's of stairs.



And I want also to add a gap between each stairs unit.
And also to make that in the Update if I change the numberOfUnits it will add/destroy more stairs units. All the stairs units should be Instantiate inside StartCoroutine.










share|improve this question



























    0















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

    public class GenerateStairs : MonoBehaviour
    {
    [Header("Stairs Prefb")]
    public GameObject stairsPrefab;
    [Space(5)]
    [Header("Platforms")]
    public bool addPlatforms = false;
    public GameObject platformsPrefab;
    [Space(5)]
    [Header("Settings")]
    [Range(1, 20)]
    public int numberOfUnits = 1;
    public float delay = 3;
    public int stairsNumber = 5;
    public Vector3 stairsStartPosition;
    public Vector3 stairSize;
    public Vector3 stairsSize;
    public float stepWidthFactor = 1f;

    private Vector3 stairsPosition;
    private GameObject stairsParent;
    private int oldNumberOfUnits = 0;

    // Use this for initialization
    void Start()
    {
    oldNumberOfUnits = numberOfUnits;

    for (int i = 0; i < numberOfUnits; i++)
    {
    stairsParent = new GameObject();
    stairsParent.name = "Stairs";
    StartCoroutine(BuildStairs());
    }
    }

    // Update is called once per frame
    void Update()
    {
    if(oldNumberOfUnits != numberOfUnits)
    {
    StartCoroutine(BuildStairs());
    oldNumberOfUnits = numberOfUnits;
    }
    }

    private IEnumerator BuildStairs()
    {
    for (int i = 1; i <= stairsNumber; i++)
    {

    stairsPosition = new Vector3(
    stairsStartPosition.x,
    stairsStartPosition.y + (i * stairsSize.y),
    stairsStartPosition.z + (i * stairsSize.y) * stepWidthFactor);

    GameObject stair = Instantiate(
    stairsPrefab,
    stairsPosition,
    Quaternion.identity);

    stair.tag = "Stair";
    stair.transform.parent = stairsParent.transform;
    stair.transform.localScale = stairSize;

    yield return new WaitForSeconds(delay);
    }

    stairsParent.AddComponent<MoveObjects>().Init();
    }
    }


    In the Start I'm doing a loop and start the Coroutine according to the numberOfunits.



    It's working fine if numberOfUnits is 1. But is it's more then 1 for example 2 it's first creating the first set of stairs but then on the second "Stairs" parent it's creating only 1 stair. I don't want it to wait to finish the first Coroutine I want in the same time to create number of Coroutine's of stairs.



    And I want also to add a gap between each stairs unit.
    And also to make that in the Update if I change the numberOfUnits it will add/destroy more stairs units. All the stairs units should be Instantiate inside StartCoroutine.










    share|improve this question

























      0












      0








      0








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

      public class GenerateStairs : MonoBehaviour
      {
      [Header("Stairs Prefb")]
      public GameObject stairsPrefab;
      [Space(5)]
      [Header("Platforms")]
      public bool addPlatforms = false;
      public GameObject platformsPrefab;
      [Space(5)]
      [Header("Settings")]
      [Range(1, 20)]
      public int numberOfUnits = 1;
      public float delay = 3;
      public int stairsNumber = 5;
      public Vector3 stairsStartPosition;
      public Vector3 stairSize;
      public Vector3 stairsSize;
      public float stepWidthFactor = 1f;

      private Vector3 stairsPosition;
      private GameObject stairsParent;
      private int oldNumberOfUnits = 0;

      // Use this for initialization
      void Start()
      {
      oldNumberOfUnits = numberOfUnits;

      for (int i = 0; i < numberOfUnits; i++)
      {
      stairsParent = new GameObject();
      stairsParent.name = "Stairs";
      StartCoroutine(BuildStairs());
      }
      }

      // Update is called once per frame
      void Update()
      {
      if(oldNumberOfUnits != numberOfUnits)
      {
      StartCoroutine(BuildStairs());
      oldNumberOfUnits = numberOfUnits;
      }
      }

      private IEnumerator BuildStairs()
      {
      for (int i = 1; i <= stairsNumber; i++)
      {

      stairsPosition = new Vector3(
      stairsStartPosition.x,
      stairsStartPosition.y + (i * stairsSize.y),
      stairsStartPosition.z + (i * stairsSize.y) * stepWidthFactor);

      GameObject stair = Instantiate(
      stairsPrefab,
      stairsPosition,
      Quaternion.identity);

      stair.tag = "Stair";
      stair.transform.parent = stairsParent.transform;
      stair.transform.localScale = stairSize;

      yield return new WaitForSeconds(delay);
      }

      stairsParent.AddComponent<MoveObjects>().Init();
      }
      }


      In the Start I'm doing a loop and start the Coroutine according to the numberOfunits.



      It's working fine if numberOfUnits is 1. But is it's more then 1 for example 2 it's first creating the first set of stairs but then on the second "Stairs" parent it's creating only 1 stair. I don't want it to wait to finish the first Coroutine I want in the same time to create number of Coroutine's of stairs.



      And I want also to add a gap between each stairs unit.
      And also to make that in the Update if I change the numberOfUnits it will add/destroy more stairs units. All the stairs units should be Instantiate inside StartCoroutine.










      share|improve this question














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

      public class GenerateStairs : MonoBehaviour
      {
      [Header("Stairs Prefb")]
      public GameObject stairsPrefab;
      [Space(5)]
      [Header("Platforms")]
      public bool addPlatforms = false;
      public GameObject platformsPrefab;
      [Space(5)]
      [Header("Settings")]
      [Range(1, 20)]
      public int numberOfUnits = 1;
      public float delay = 3;
      public int stairsNumber = 5;
      public Vector3 stairsStartPosition;
      public Vector3 stairSize;
      public Vector3 stairsSize;
      public float stepWidthFactor = 1f;

      private Vector3 stairsPosition;
      private GameObject stairsParent;
      private int oldNumberOfUnits = 0;

      // Use this for initialization
      void Start()
      {
      oldNumberOfUnits = numberOfUnits;

      for (int i = 0; i < numberOfUnits; i++)
      {
      stairsParent = new GameObject();
      stairsParent.name = "Stairs";
      StartCoroutine(BuildStairs());
      }
      }

      // Update is called once per frame
      void Update()
      {
      if(oldNumberOfUnits != numberOfUnits)
      {
      StartCoroutine(BuildStairs());
      oldNumberOfUnits = numberOfUnits;
      }
      }

      private IEnumerator BuildStairs()
      {
      for (int i = 1; i <= stairsNumber; i++)
      {

      stairsPosition = new Vector3(
      stairsStartPosition.x,
      stairsStartPosition.y + (i * stairsSize.y),
      stairsStartPosition.z + (i * stairsSize.y) * stepWidthFactor);

      GameObject stair = Instantiate(
      stairsPrefab,
      stairsPosition,
      Quaternion.identity);

      stair.tag = "Stair";
      stair.transform.parent = stairsParent.transform;
      stair.transform.localScale = stairSize;

      yield return new WaitForSeconds(delay);
      }

      stairsParent.AddComponent<MoveObjects>().Init();
      }
      }


      In the Start I'm doing a loop and start the Coroutine according to the numberOfunits.



      It's working fine if numberOfUnits is 1. But is it's more then 1 for example 2 it's first creating the first set of stairs but then on the second "Stairs" parent it's creating only 1 stair. I don't want it to wait to finish the first Coroutine I want in the same time to create number of Coroutine's of stairs.



      And I want also to add a gap between each stairs unit.
      And also to make that in the Update if I change the numberOfUnits it will add/destroy more stairs units. All the stairs units should be Instantiate inside StartCoroutine.







      c# unity3d






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '18 at 20:36









      Benzi AvrumiBenzi Avrumi

      39110




      39110
























          1 Answer
          1






          active

          oldest

          votes


















          2














          You are mistaking how the coroutine works its not at thread. What is happening is your continually invoking the coroutine so its starting over and over again not creating a separate instance.



          what you should do is create create a prefab and Instantiate that to do the work. My last remark was about threads but you wont be able to instantiate anything unless its on the main thread so the easiest way to get this done would be like so.



          public GameObject yourGoWithAboveClassOnIt;


          void Start()
          {
          oldNumberOfUnits = numberOfUnits;

          for (int i = 0; i < numberOfUnits; i++)
          {
          Instantiate(yourGoWithAboveClassOnIt);
          }
          }


          your prior class will remove this



          void Start()
          {
          //oldNumberOfUnits = numberOfUnits;

          //for (int i = 0; i < numberOfUnits; i++)
          //{
          stairsParent = new GameObject();
          stairsParent.name = "Stairs";
          StartCoroutine(BuildStairs());
          //}
          }





          share|improve this answer


























          • Right, Loop over the prefab and duplicate/create many gameobjects with the GenerateStairs script on each one.

            – Benzi Avrumi
            Nov 19 '18 at 21:09






          • 1





            high five :) and happy coding.

            – Levon Ravel
            Nov 19 '18 at 21:21











          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: "1"
          };
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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%2fstackoverflow.com%2fquestions%2f53382263%2fhow-can-i-generate-the-units-at-the-same-time%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














          You are mistaking how the coroutine works its not at thread. What is happening is your continually invoking the coroutine so its starting over and over again not creating a separate instance.



          what you should do is create create a prefab and Instantiate that to do the work. My last remark was about threads but you wont be able to instantiate anything unless its on the main thread so the easiest way to get this done would be like so.



          public GameObject yourGoWithAboveClassOnIt;


          void Start()
          {
          oldNumberOfUnits = numberOfUnits;

          for (int i = 0; i < numberOfUnits; i++)
          {
          Instantiate(yourGoWithAboveClassOnIt);
          }
          }


          your prior class will remove this



          void Start()
          {
          //oldNumberOfUnits = numberOfUnits;

          //for (int i = 0; i < numberOfUnits; i++)
          //{
          stairsParent = new GameObject();
          stairsParent.name = "Stairs";
          StartCoroutine(BuildStairs());
          //}
          }





          share|improve this answer


























          • Right, Loop over the prefab and duplicate/create many gameobjects with the GenerateStairs script on each one.

            – Benzi Avrumi
            Nov 19 '18 at 21:09






          • 1





            high five :) and happy coding.

            – Levon Ravel
            Nov 19 '18 at 21:21
















          2














          You are mistaking how the coroutine works its not at thread. What is happening is your continually invoking the coroutine so its starting over and over again not creating a separate instance.



          what you should do is create create a prefab and Instantiate that to do the work. My last remark was about threads but you wont be able to instantiate anything unless its on the main thread so the easiest way to get this done would be like so.



          public GameObject yourGoWithAboveClassOnIt;


          void Start()
          {
          oldNumberOfUnits = numberOfUnits;

          for (int i = 0; i < numberOfUnits; i++)
          {
          Instantiate(yourGoWithAboveClassOnIt);
          }
          }


          your prior class will remove this



          void Start()
          {
          //oldNumberOfUnits = numberOfUnits;

          //for (int i = 0; i < numberOfUnits; i++)
          //{
          stairsParent = new GameObject();
          stairsParent.name = "Stairs";
          StartCoroutine(BuildStairs());
          //}
          }





          share|improve this answer


























          • Right, Loop over the prefab and duplicate/create many gameobjects with the GenerateStairs script on each one.

            – Benzi Avrumi
            Nov 19 '18 at 21:09






          • 1





            high five :) and happy coding.

            – Levon Ravel
            Nov 19 '18 at 21:21














          2












          2








          2







          You are mistaking how the coroutine works its not at thread. What is happening is your continually invoking the coroutine so its starting over and over again not creating a separate instance.



          what you should do is create create a prefab and Instantiate that to do the work. My last remark was about threads but you wont be able to instantiate anything unless its on the main thread so the easiest way to get this done would be like so.



          public GameObject yourGoWithAboveClassOnIt;


          void Start()
          {
          oldNumberOfUnits = numberOfUnits;

          for (int i = 0; i < numberOfUnits; i++)
          {
          Instantiate(yourGoWithAboveClassOnIt);
          }
          }


          your prior class will remove this



          void Start()
          {
          //oldNumberOfUnits = numberOfUnits;

          //for (int i = 0; i < numberOfUnits; i++)
          //{
          stairsParent = new GameObject();
          stairsParent.name = "Stairs";
          StartCoroutine(BuildStairs());
          //}
          }





          share|improve this answer















          You are mistaking how the coroutine works its not at thread. What is happening is your continually invoking the coroutine so its starting over and over again not creating a separate instance.



          what you should do is create create a prefab and Instantiate that to do the work. My last remark was about threads but you wont be able to instantiate anything unless its on the main thread so the easiest way to get this done would be like so.



          public GameObject yourGoWithAboveClassOnIt;


          void Start()
          {
          oldNumberOfUnits = numberOfUnits;

          for (int i = 0; i < numberOfUnits; i++)
          {
          Instantiate(yourGoWithAboveClassOnIt);
          }
          }


          your prior class will remove this



          void Start()
          {
          //oldNumberOfUnits = numberOfUnits;

          //for (int i = 0; i < numberOfUnits; i++)
          //{
          stairsParent = new GameObject();
          stairsParent.name = "Stairs";
          StartCoroutine(BuildStairs());
          //}
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 '18 at 21:06

























          answered Nov 19 '18 at 20:53









          Levon RavelLevon Ravel

          1805




          1805













          • Right, Loop over the prefab and duplicate/create many gameobjects with the GenerateStairs script on each one.

            – Benzi Avrumi
            Nov 19 '18 at 21:09






          • 1





            high five :) and happy coding.

            – Levon Ravel
            Nov 19 '18 at 21:21



















          • Right, Loop over the prefab and duplicate/create many gameobjects with the GenerateStairs script on each one.

            – Benzi Avrumi
            Nov 19 '18 at 21:09






          • 1





            high five :) and happy coding.

            – Levon Ravel
            Nov 19 '18 at 21:21

















          Right, Loop over the prefab and duplicate/create many gameobjects with the GenerateStairs script on each one.

          – Benzi Avrumi
          Nov 19 '18 at 21:09





          Right, Loop over the prefab and duplicate/create many gameobjects with the GenerateStairs script on each one.

          – Benzi Avrumi
          Nov 19 '18 at 21:09




          1




          1





          high five :) and happy coding.

          – Levon Ravel
          Nov 19 '18 at 21:21





          high five :) and happy coding.

          – Levon Ravel
          Nov 19 '18 at 21:21




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • 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.


          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%2fstackoverflow.com%2fquestions%2f53382263%2fhow-can-i-generate-the-units-at-the-same-time%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







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini