c# unity instantiate code example

Example 1: Unity C# instantiate prefab

Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);

Example 2: 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 3: 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 4: 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);
        }
    }
}