jonathan gibson
jonathan gibson

Reputation: 1

Unity 3D: Instantiate GameObject to specific position on the Parent

Newbie here. I want to Instantiate a GameObject to a specific position on the Parent. I want to place it at the top of the parent. Can I position it immediately when I instantiate it or do I need to use the transform.position? In either case I don't know how to do it. I also need to figure out how to rotate the child on the parent if you are feeling generous with your time. Also, each child/copy or new instantiated object will scale with each new iteration. I'm trying to build a reverse fractal tree (the branches get bigger over time).

Just a warning - you might cringe at other parts of the code that probably could be written better.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    public Transform Cube;
    public GameObject masterTree;
    public int instanceCounter = 1;
    public int numCubes = 30;
    public float scalar = 1.4145f;
    public float initialScale = 10f;
    public float angle = 30f;
    private Transform copy;

    void Start()
    {

    }

    private void Update()
    {
        if (instanceCounter <= numCubes)
        {
            if (instanceCounter == 1)
            {
                copy = Instantiate(Cube, new Vector3(0, 0, 0), Quaternion.identity);
                copy.transform.localScale = new Vector3(1f, initialScale, 1f);
                copy.name = "Copy" + instanceCounter;
                copy.transform.parent = masterTree.transform;
                instanceCounter++;
            }

            var copyParent = GameObject.Find("Copy" + (instanceCounter - 1));
            Vector3 copyParentSize = copyParent.GetComponent<Renderer>().bounds.size;
            Debug.Log("copyParentSizeY = " + copyParentSize.y);

            copy = Instantiate(Cube, new Vector3(0, 0, 0), Quaternion.identity);

            copy.transform.localScale = new Vector3(1f, initialScale, 1f);
            initialScale = initialScale * scalar;
            copy.name = "Copy" + instanceCounter;

            //copy.transform.rotation *= Quaternion.Euler(angle, angle, 0);
            copy.transform.parent = copyParent.transform;
            instanceCounter++;
        }
    }
}

Upvotes: 0

Views: 4020

Answers (2)

jonathan gibson
jonathan gibson

Reputation: 1

I understand Vector3 better now. It looks like I can add together different GameObject positions together in the same Vector3:

newPosition = new Vector3(0, copyParentSize.y + copyParentPosition.y + spacer, 0);

Now I move objects around based on the location of other objects. Thank you for your help!

Upvotes: 0

If I understands your needs right... There is a Instantiate method with Parent parameter, so you can create new GO as a child of your parent

public static Object Instantiate(Object original, Transform parent);

If you want to have some kind of pivot, you can create empty GameObject in your target parent, shift it to right position and instantiate your GO as a child of that empty GO.

Also you can wrap your Cube in another empty GameObject (ex: position 0,0,0), so you can shift Cube up (0,5,0), but origin of whole GameObject remains same (0,0,0).

Upvotes: 1

Related Questions