• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by alexander11 · Aug 08, 2016 at 04:57 AM · c#unity 5gameobjectinstantiate

How do i Instantiate Objects to a Empty GameObject as a Child??

Hello i am having some trouble trying to achieve this in Pic1 where i can move the other cubes along with the base one, but i cant seem to achieve that, here are my results in Pic2. does anyone know how to do this ?

Pic1. alt text

Pic2. alt text

Here is my code so you know what i have done.

 using UnityEngine;
 using System.Collections;
 
 public class InstantiateCubes : MonoBehaviour {
 
     public int xSize, zSize;
     private Vector3[] points;
     public GameObject cube;
     public int offset;
     void Start () {
         points = new Vector3[(xSize + 1) * (zSize + 1)];
         for(int i = 0, x = 0; x < xSize; x++)
         {
             for(int z = 0; z < zSize; z++, i++)
             {
                 points[i] = new Vector3(x * offset, 0, z * offset);
                 Instantiate(cube, points[i], Quaternion.identity);
             }
         }
     }
 }

@StupidlySimple

capture06.png (40.4 kB)
capture07.png (35.9 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by arko · Aug 08, 2016 at 02:12 PM

You can have the first cube as the parent of the other cubes or have an empty game object as the parent of all the cubes. Lets say you want to use the first cube (instantiated at x=0,z=0) to be the parent. All you have to do when you instantiate the first cube, you save its reference and then when instantiating other cubes set their parent to the first cube. The modified code snapshot would be like this

 void Start () {
          points = new Vector3[(xSize + 1) * (zSize + 1)];
          
          GameObject boxParent; //The parent
 
          for(int i = 0, x = 0; x < xSize; x++)
          {
              for(int z = 0; z < zSize; z++, i++)
              {
                  points[i] = new Vector3(x * offset, 0, z * offset);
                  GameObject go =  (GameObject) Instantiate(cube, points[i], Quaternion.identity);
 
                  if(x==0  && z==0) 
                  {
                           boxParent = go;
                  }
                  else
                  {
                            go.transform.SetParent(boxParent.transform);
                  }
              }
          }
      }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image alexander11 · Aug 09, 2016 at 12:37 AM 0
Share

Thank you very much.

avatar image
0

Answer by StupidlySimple · Aug 08, 2016 at 06:28 AM

     public class InstantiateCubes : MonoBehaviour
     {
         public int xSize, zSize;
         private Vector3[] points;
         public GameObject cube;
         public int offset;
         void Start()
         {
             points = new Vector3[(xSize + 1) * (zSize + 1)];
             for (int i = 0, x = 0; x < xSize; x++)
             {
                 for (int z = 0; z < zSize; z++, i++)
                 {
                     points[i] = new Vector3(x * offset, 0, z * offset);
                     (Instantiate(cube, points[i], Quaternion.identity) as GameObject).transform.parent = cube.transform;
                 }
             }
         }
     }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image StupidlySimple · Aug 08, 2016 at 06:29 AM 0
Share

There's probably elegant way to do it, but I'm lazy.

avatar image alexander11 StupidlySimple · Aug 08, 2016 at 06:48 AM 0
Share

Lol. Its kind of working but its putting child obj with in child obj. alt text

capture08.png (15.7 kB)
avatar image StupidlySimple alexander11 · Aug 08, 2016 at 07:28 AM 0
Share

Of course it is because you're duplicating cube as child of a cube.

Cube1 start with script will make copy of itself and add it as child so:

Cube1 --- Cube2

The moment Cube2 created, the script get triggered and force it to create another cube, thus

Cube1 --- Cube2 ------- Cube3

And so on.

Lol... anyway, the idea is set all those three cubes as child of the first cube via transform.parent. That way the first cube move in world space, the three will followed.

Show more comments
avatar image
0

Answer by saldavonschwartz · Aug 08, 2016 at 08:25 AM

@alexander11

As was mentioned in the other answer, parenting is achieved by means of the transforms of objects. If you have GameObject's A and B and B.transform.parent = A.transform B is a child of A.

From your drawing it is not entirely clear to me if you are trying to make all cubes the children of 'Base' while keeping their relative positions or if you want something else. But just in case, this might help you:

You might want to express an object's transform relative to the frame of reference established by the parent's transform.

That is, if A is at 0,0,0 and B is at 1,0,0 (1 unit to the right from it's parent), you probably want things set up in such way that if you then move A to 1,0,0 B ends up at 2,0,0 and not still at 1,0,0. To draw this distinction, the transform component has postion and localPosition. If you want the child's position relative to the parent, you should use localPosition.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

232 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i Instantiate gameObjects in between multiple points? 2 Answers

Creating a GameObject variable without instantiating it? 1 Answer

How can I check if an instantiated object collides with another instantiated object? 1 Answer

How to instantiate gameobjects through udp? 0 Answers

how to make a Login Window with unity and c# 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges