instantiate object inside of object Unity code example
Example 1: unity instantiate an object
// Instantiates 10 copies of Prefab each 2 units apart from each otherusing UnityEngine;
using System.Collections;public class ExampleClass : MonoBehaviour
{
public Transform prefab;
void Start()
{
for (int i = 0; i < 10; i++)
{
Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);
}
}
}
Example 2: instantiate object unity
public static Object Instantiate(Object original);
public static Object Instantiate(Object original, Transform parent);
public static Object Instantiate(Object original, Transform parent, bool instantiateInWorldSpace);
public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);
public static Object Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);
Example 3: instantiate object inside of object Unity
// 1. create game object
// 2. set its transform.parent property
GameObject child = GameObject.Instantiate(MyPrefab);
child.transform.parent = this.gameObject.transform; //or whatever gameobj will be its parent
Example 4: instantiate object inside of object Unity
// Makes the camera follow this object by
// making it a child of this transform.
// Get the transform of the camera
var cameraTransform = Camera.main.transform;
// make it a child of the current object
cameraTransform.parent = transform;
// place it behind the current object
cameraTransform.localPosition = -Vector3.forward * 5;
// make it point towards the object
cameraTransform.LookAt(transform);
Example 5: instantiate object inside of object Unity
var newObj = GameObject.Instantiate(somePrefabReference);
newObj.transform.parent = GameObject.Find("EmptyGameObject").transform;