How can I add as child to a parent each group of stairs to it's correct parent?












0















In the first script:



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

public class GenerateStairsUnits : MonoBehaviour
{
[Header("Stairs Units Prefab")]
public GameObject stairsUnitsPrefab;
[Space(5)]
[Header("Settings")]
[Range(1, 20)]
public int numberOfUnits = 1;
public static GameObject Unit;

private int oldNumberOfUnits = 0;

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

var unitsParent = GameObject.Find("Stairs Units");
for (int i = 0; i < numberOfUnits; i++)
{
Unit = Instantiate(stairsUnitsPrefab, unitsParent.transform);
Unit.name = "Stairs " + i.ToString();
}
}

// Update is called once per frame
void Update ()
{

}
}


So now under Stairs Units I have for example 5 Stairs (Stairs 0 , Stairs 1, Stairs 2, Stairs 3, Stairs 4) that are a copy of this script:



using System.Collections;
using System.Collections.Generic;
using System.Threading;
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")]
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;

// Use this for initialization
void Start()
{
stairsParent = new GameObject();
stairsParent.name = "Stairs";
stairsParent.transform.parent = GenerateStairsUnits.Unit.transform;
StartCoroutine(BuildStairs());
}

// Update is called once per frame
void Update()
{

}

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();
}
}


Now on the second script the "Stairs" I want to be a child of each stairs unit:
For example under Stairs 0 should be Stairs. Then under Stairs 1 there should be also Stairs and also under Stairs 2 and Stairs 3 and Stairs 4.



But the way I tried to do it:



stairsParent.transform.parent = GenerateStairsUnits.Unit.transform;


Don't put each Stairs under Stairs 0,1,2,3,4










share|improve this question



























    0















    In the first script:



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

    public class GenerateStairsUnits : MonoBehaviour
    {
    [Header("Stairs Units Prefab")]
    public GameObject stairsUnitsPrefab;
    [Space(5)]
    [Header("Settings")]
    [Range(1, 20)]
    public int numberOfUnits = 1;
    public static GameObject Unit;

    private int oldNumberOfUnits = 0;

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

    var unitsParent = GameObject.Find("Stairs Units");
    for (int i = 0; i < numberOfUnits; i++)
    {
    Unit = Instantiate(stairsUnitsPrefab, unitsParent.transform);
    Unit.name = "Stairs " + i.ToString();
    }
    }

    // Update is called once per frame
    void Update ()
    {

    }
    }


    So now under Stairs Units I have for example 5 Stairs (Stairs 0 , Stairs 1, Stairs 2, Stairs 3, Stairs 4) that are a copy of this script:



    using System.Collections;
    using System.Collections.Generic;
    using System.Threading;
    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")]
    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;

    // Use this for initialization
    void Start()
    {
    stairsParent = new GameObject();
    stairsParent.name = "Stairs";
    stairsParent.transform.parent = GenerateStairsUnits.Unit.transform;
    StartCoroutine(BuildStairs());
    }

    // Update is called once per frame
    void Update()
    {

    }

    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();
    }
    }


    Now on the second script the "Stairs" I want to be a child of each stairs unit:
    For example under Stairs 0 should be Stairs. Then under Stairs 1 there should be also Stairs and also under Stairs 2 and Stairs 3 and Stairs 4.



    But the way I tried to do it:



    stairsParent.transform.parent = GenerateStairsUnits.Unit.transform;


    Don't put each Stairs under Stairs 0,1,2,3,4










    share|improve this question

























      0












      0








      0








      In the first script:



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

      public class GenerateStairsUnits : MonoBehaviour
      {
      [Header("Stairs Units Prefab")]
      public GameObject stairsUnitsPrefab;
      [Space(5)]
      [Header("Settings")]
      [Range(1, 20)]
      public int numberOfUnits = 1;
      public static GameObject Unit;

      private int oldNumberOfUnits = 0;

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

      var unitsParent = GameObject.Find("Stairs Units");
      for (int i = 0; i < numberOfUnits; i++)
      {
      Unit = Instantiate(stairsUnitsPrefab, unitsParent.transform);
      Unit.name = "Stairs " + i.ToString();
      }
      }

      // Update is called once per frame
      void Update ()
      {

      }
      }


      So now under Stairs Units I have for example 5 Stairs (Stairs 0 , Stairs 1, Stairs 2, Stairs 3, Stairs 4) that are a copy of this script:



      using System.Collections;
      using System.Collections.Generic;
      using System.Threading;
      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")]
      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;

      // Use this for initialization
      void Start()
      {
      stairsParent = new GameObject();
      stairsParent.name = "Stairs";
      stairsParent.transform.parent = GenerateStairsUnits.Unit.transform;
      StartCoroutine(BuildStairs());
      }

      // Update is called once per frame
      void Update()
      {

      }

      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();
      }
      }


      Now on the second script the "Stairs" I want to be a child of each stairs unit:
      For example under Stairs 0 should be Stairs. Then under Stairs 1 there should be also Stairs and also under Stairs 2 and Stairs 3 and Stairs 4.



      But the way I tried to do it:



      stairsParent.transform.parent = GenerateStairsUnits.Unit.transform;


      Don't put each Stairs under Stairs 0,1,2,3,4










      share|improve this question














      In the first script:



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

      public class GenerateStairsUnits : MonoBehaviour
      {
      [Header("Stairs Units Prefab")]
      public GameObject stairsUnitsPrefab;
      [Space(5)]
      [Header("Settings")]
      [Range(1, 20)]
      public int numberOfUnits = 1;
      public static GameObject Unit;

      private int oldNumberOfUnits = 0;

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

      var unitsParent = GameObject.Find("Stairs Units");
      for (int i = 0; i < numberOfUnits; i++)
      {
      Unit = Instantiate(stairsUnitsPrefab, unitsParent.transform);
      Unit.name = "Stairs " + i.ToString();
      }
      }

      // Update is called once per frame
      void Update ()
      {

      }
      }


      So now under Stairs Units I have for example 5 Stairs (Stairs 0 , Stairs 1, Stairs 2, Stairs 3, Stairs 4) that are a copy of this script:



      using System.Collections;
      using System.Collections.Generic;
      using System.Threading;
      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")]
      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;

      // Use this for initialization
      void Start()
      {
      stairsParent = new GameObject();
      stairsParent.name = "Stairs";
      stairsParent.transform.parent = GenerateStairsUnits.Unit.transform;
      StartCoroutine(BuildStairs());
      }

      // Update is called once per frame
      void Update()
      {

      }

      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();
      }
      }


      Now on the second script the "Stairs" I want to be a child of each stairs unit:
      For example under Stairs 0 should be Stairs. Then under Stairs 1 there should be also Stairs and also under Stairs 2 and Stairs 3 and Stairs 4.



      But the way I tried to do it:



      stairsParent.transform.parent = GenerateStairsUnits.Unit.transform;


      Don't put each Stairs under Stairs 0,1,2,3,4







      c# unity3d






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '18 at 22:47









      Benzi AvrumiBenzi Avrumi

      39110




      39110
























          1 Answer
          1






          active

          oldest

          votes


















          1














          So what I can see is your instantiating 5 Stairs each containing the class that instantiates more stairs. In other words Stairs# is the transform the other stairs need to transform to. Your setting the other stairs currently to Stairs Unit all you need to do is the Stair# transform without .parent. Also in start remove. A quick mock up Stairs Unit/Stair# <--- transform.parent would set the other stairs to Stairs Unit what I think you are after is Stairs Unit/Stairs#/more stairs, hope that helps with the small visual.



          stairsParent.transform.parent = GenerateStairsUnits.Unit.transform;


          then



          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.SetParent(transform);//just use this transform I believe this would be Stairs#
          stair.transform.localScale = stairSize;

          yield return new WaitForSeconds(delay);
          }

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





          share|improve this answer

























            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%2f53383753%2fhow-can-i-add-as-child-to-a-parent-each-group-of-stairs-to-its-correct-parent%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









            1














            So what I can see is your instantiating 5 Stairs each containing the class that instantiates more stairs. In other words Stairs# is the transform the other stairs need to transform to. Your setting the other stairs currently to Stairs Unit all you need to do is the Stair# transform without .parent. Also in start remove. A quick mock up Stairs Unit/Stair# <--- transform.parent would set the other stairs to Stairs Unit what I think you are after is Stairs Unit/Stairs#/more stairs, hope that helps with the small visual.



            stairsParent.transform.parent = GenerateStairsUnits.Unit.transform;


            then



            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.SetParent(transform);//just use this transform I believe this would be Stairs#
            stair.transform.localScale = stairSize;

            yield return new WaitForSeconds(delay);
            }

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





            share|improve this answer






























              1














              So what I can see is your instantiating 5 Stairs each containing the class that instantiates more stairs. In other words Stairs# is the transform the other stairs need to transform to. Your setting the other stairs currently to Stairs Unit all you need to do is the Stair# transform without .parent. Also in start remove. A quick mock up Stairs Unit/Stair# <--- transform.parent would set the other stairs to Stairs Unit what I think you are after is Stairs Unit/Stairs#/more stairs, hope that helps with the small visual.



              stairsParent.transform.parent = GenerateStairsUnits.Unit.transform;


              then



              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.SetParent(transform);//just use this transform I believe this would be Stairs#
              stair.transform.localScale = stairSize;

              yield return new WaitForSeconds(delay);
              }

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





              share|improve this answer




























                1












                1








                1







                So what I can see is your instantiating 5 Stairs each containing the class that instantiates more stairs. In other words Stairs# is the transform the other stairs need to transform to. Your setting the other stairs currently to Stairs Unit all you need to do is the Stair# transform without .parent. Also in start remove. A quick mock up Stairs Unit/Stair# <--- transform.parent would set the other stairs to Stairs Unit what I think you are after is Stairs Unit/Stairs#/more stairs, hope that helps with the small visual.



                stairsParent.transform.parent = GenerateStairsUnits.Unit.transform;


                then



                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.SetParent(transform);//just use this transform I believe this would be Stairs#
                stair.transform.localScale = stairSize;

                yield return new WaitForSeconds(delay);
                }

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





                share|improve this answer















                So what I can see is your instantiating 5 Stairs each containing the class that instantiates more stairs. In other words Stairs# is the transform the other stairs need to transform to. Your setting the other stairs currently to Stairs Unit all you need to do is the Stair# transform without .parent. Also in start remove. A quick mock up Stairs Unit/Stair# <--- transform.parent would set the other stairs to Stairs Unit what I think you are after is Stairs Unit/Stairs#/more stairs, hope that helps with the small visual.



                stairsParent.transform.parent = GenerateStairsUnits.Unit.transform;


                then



                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.SetParent(transform);//just use this transform I believe this would be Stairs#
                stair.transform.localScale = stairSize;

                yield return new WaitForSeconds(delay);
                }

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






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 20 '18 at 0:15

























                answered Nov 19 '18 at 23:39









                Levon RavelLevon Ravel

                1805




                1805






























                    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%2f53383753%2fhow-can-i-add-as-child-to-a-parent-each-group-of-stairs-to-its-correct-parent%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

                    Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                    ComboBox Display Member on multiple fields

                    Is it possible to collect Nectar points via Trainline?