unity instantiate code example

Example 1: unity create gameobject from prefab in script

using UnityEngine;

public class example : MonoBehaviour
{
	public GameObject prefab;
    
	// Start is called before the first frame update
	void Start()
	{
		Instantiate(prefab, new Vector3(0, 0, 0), Quaternion.identity);
	}
}

Example 2: unity instantiate

// You can use the 'Instantiate()' function to clone a GameObject
clone = Instantiate(original);

// You can also set the position, rotation and parent
clone = Instantiate(original, new Vector3(0, 0, 0), Quaternion.identity, cloneParent.transform);

Example 3: c# instantiate

// 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 4: untiy instanciate prefab

Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);

Example 5: how to instantiate a gameobject

/// <summary>
/// Creates a new Gameobject prefab called Name_1 for example
/// Instantiates the prefab
/// </summary>
public void AddGameObject()
{
	//created for example only
	GameObject testPrefab = new GameObject("Name_1");
    Vector3 objectPOS = Vector3.zero;

	GameObject newGameObject = Instantiate(testPrefab, objectPOS, Quaternion.identity);
}

Example 6: 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);