instantiate prefab unity 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: instantiate prefab

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

Example 5: unity instantiate prefab

Instantiate(cube, Vector3 (x, y, 0), Quaternion.identity);

Example 6: unity instantiate vector3

using UnityEngine;// Instantiate a rigidbody then set the velocitypublic class Example : MonoBehaviour
{
    // Assign a Rigidbody component in the inspector to instantiate    public Rigidbody projectile;    void Update()
    {
        // Ctrl was pressed, launch a projectile
        if (Input.GetButtonDown("Fire1"))
        {
            // Instantiate the projectile at the position and rotation of this transform
            Rigidbody clone;
            clone = Instantiate(projectile, transform.position, transform.rotation);            // Give the cloned object an initial velocity along the current
            // object's Z axis
            clone.velocity = transform.TransformDirection(Vector3.forward * 10);
        }
    }
}