---
title: "Instantiating AR Models into a Virtual Scene"
content_type: tutorial
url: "https://learn.unity.com/tutorial/instantiating-ar-models-into-a-virtual-scene?language=en"
language: en
difficulty: intermediate
duration_minutes: 20
unity_version:
  - "2019.4"
supported_unity_versions:
  - name: "2019.4"
    url: "https://learn.unity.com/tutorial/instantiating-ar-models-into-a-virtual-scene.md?language=en&version=2019.4"
  - name: "2019.3"
    url: "https://learn.unity.com/tutorial/instantiating-ar-models-into-a-virtual-scene.md?language=en&version=2019.3"
  - name: "2019.2"
    url: "https://learn.unity.com/tutorial/instantiating-ar-models-into-a-virtual-scene.md?language=en&version=2019.2"
  - name: "2019.1"
    url: "https://learn.unity.com/tutorial/instantiating-ar-models-into-a-virtual-scene.md?language=en&version=2019.1"
last_updated: "2026-08-04T14:24:16.000Z"
---

# Instantiating AR Models into a Virtual Scene

Discover how to spawn and position instantiated objects to drive interactive AR experiences.

Instantiating objects, also known as spawning, and adjusting their positions are foundational features of interactive AR applications. Although these applications may serve different use cases with different visual styles, the logic behind object spawning is typically very consistent.

## Instantiating AR Models into a Virtual Scene

_This tutorial has been verified using Unity 2019.4 LTS, AR Foundation 2.1.10 and ARCore XR Plugin 2.1.12_

Instantiating objects, also known as spawning, and adjusting their positions are foundational features of interactive AR applications. Although these applications may serve different use cases with different visual styles, the logic behind object spawning is typically very consistent.

![Figure 01: Wayfair uses AR to let users visualize what furniture would look like in their homes before purchasing it.](https://connect-mediagw.unity.com/h1/20210224/learn/images/93e1d36e-6f8b-4889-b0f4-9bd5888ce72b_image1.jpg)

In essence, an AR application with object-spawning features must be able to:

- Detect planes in your play area.
- Spawn and move an object after you press on the plane.
- Spawn multiple objects around the plane after you tap on other areas.

## Installing the AR Foundation Package

1\. Go to **Window** > **Package Manager**.

2\. Once the Package Manager window is open, ensure that you can see the packages in the **Unity Registry** (**Figure 02**).

![Figure 02: Package Manager window showing the Unity Registry](https://connect-mediagw.unity.com/h1/20210224/learn/images/b495a2e9-3bd3-4f75-be09-fd999bbacd35_image7.jpg)

3\. Search for **AR Foundation** and click **Install** (**Figure 03**).

![Figure 03: Installing the AR Foundation package](https://connect-mediagw.unity.com/h1/20210224/learn/images/39da8449-dc8b-486e-bee6-af6584538219_image10.jpg)

4\. You now have the **AR Foundation** package in your project.

5\. For this project, we will be using an Android device, so install the **ARCore XR Plugin**.

## Configuring Your Scene for AR Foundation and Plane Detection

1\. Add the AR Session to your Scene by clicking **GameObject** > **XR** > **AR Session** (**Figure 04**).

![Figure 04: Adding the AR Session to the Scene](https://connect-mediagw.unity.com/h1/20210224/learn/images/ab3d9270-cf61-4699-aa9d-1e0f748b3b16_image12.jpg)

2\. If your AR session doesn’t have an AR Input Manager, add it by clicking **Add Component** and searching for **AR Input Manager** (**Figure 05**). The AR Input Manager handles all of your scene input so you can interact with trackable objects, such as planes and feature points.

![Figure 05: AR Session GameObject with the AR Input Manager](https://connect-mediagw.unity.com/h1/20210224/learn/images/ddfcb5fc-1a10-453e-bb9d-c808c86aea16_image13.jpg)

3\. Add the AR Session Origin GameObject to the Scene by clicking **GameObject** > **XR** > **AR Session Origin** (**Figure 06**).

![Figure 06: Adding the AR Session Origin to the Scene](https://connect-mediagw.unity.com/h1/20210224/learn/images/90264671-5bad-41a6-ab78-0c895080dfa8_image6.jpg)

4\. In the AR Session Origin Inspector, make sure **AR Camera** is set as the reference in the AR Session Origin Camera parameter (**Figure 07**).

![Figure 07: AR Session Origin Camera field](https://connect-mediagw.unity.com/h1/20210224/learn/images/c0901cad-d86e-4b94-bd1a-ae7d96e959b1_image16.jpg)

5\. To enable plane detection in your AR Scene, add the **AR Plane Manager** to the **AR Session Origin** object.

6\. Specify the **AR Plane Manage**r to detect vertical and horizontal planes by adjusting its **DetectionMode** to **Everything**.

7\. To interact with trackable features (such as planes), add the **AR Raycast Manager**.

8\. Your AR Session Origin object should look like the following (**Figure 08**):

![Figure 08: Add and configure the AR Session Origin](https://connect-mediagw.unity.com/h1/20210224/learn/images/7968ae5b-0783-46d6-a83e-8f1d2adfd5a9_image5.jpg)

9\. We’ll need a plane Prefab so the AR Plane Manager script can display the location of the planes in our AR Scene. In the Hierarchy**,** right-click and select **XR** > **AR Default Plane** (**Figure 08**).

![Figure 08: Adding the AR Default Plane](https://connect-mediagw.unity.com/h1/20210224/learn/images/a460f963-a0ba-451c-8b17-0e6398e2f3dd_image15.jpg)

10\. Drag this new plane from the Hierarchy into the Project window to create a Prefab and delete it from the Hierarchy.

11\. In the AR Plane Manager script, assign the **AR Default Plane Prefab** from the Project window to the **Plane Prefab** variable in the Inspector (**Figure 09**).

![Figure 09: Assigning the AR Default Plane prefab](https://connect-mediagw.unity.com/h1/20210224/learn/images/ae1a9cba-4f83-48be-8664-2436f6f3ad60_image8.jpg)

12\. We need an object to spawn in our Scene. Create a Cube and name it _SpawnableObject_.

13\. Set the scale to **0.1**, **0.1**, **0.1**

14\. Create a tag named _Spawnable_ and assign it to **SpawnableObject**. You can use that tag in future projects to tell when a ray hits one of these Prefabs.

15\. Drag SpawnableObject from the Hierarchy into the Project window to create a Prefab and then delete SpawnableObject from the Hierarchy (**Figure 10**).

![Figure 10: The completed SpawnableObject Prefab](https://connect-mediagw.unity.com/h1/20210224/learn/images/cf3e2643-a91a-4a4b-8308-8c6bb910824f_image11.jpg)

## Creating the Interaction Script

1\. Create a C# script and name it _SpawnableManager_.

2\. Create an Empty Game Object in your Scene and name it _SpawnManager_.

3\. Assign the SpawnableManager script to your SpawnManager object.

4\. Open the SpawnableManager script.

5\. At the top, add:

```csharp

 using UnityEngine.XR.ARFoundation;
```

6\. Now we’ll create some variables to the top of the script’s class:

```csharp
[SerializeField]
ARRaycastManager m_RaycastManager;
List<ARRaycastHit> m_Hits = new List<ARRaycastHit>();
[SerializeField]
GameObject spawnablePrefab;

GameObject spawnedObject;
```

7\. Inside the Start method, we’ll set the spawnedObject variable to null. Add this line to the Start method:

```csharp
spawnedObject = null;
```

8\. In the Update method, we’ll check if there is a touch event and, if so, we will:

- Check to see if a ray has hit a plane.
- Check to see which phase our touch event is at.
- If the phase is set to **Begin**, we will instantiate our Prefab and assign that object to our spawnedObject variable.
- If the phase is set to **Moved** and our spawnedObject variable has something in it, we’ll move the spawnedObject to the touch location on the plane.
- If the phase is **Ended**, we’ll set the spawnedObject variable to null so we no longer are able to drag the Prefab around.

9\. Add the following code to the Update method:

```csharp
if (Input.touchCount == 0)
    return;

if (m_RaycastManager.Raycast(Input.GetTouch(0).position, m_Hits))
{
    if(Input.GetTouch(0).phase == TouchPhase.Began)
    {
        SpawnPrefab(m_Hits[0].pose.position);
    }
    else if(Input.GetTouch(0).phase == TouchPhase.Moved && spawnedObject != null)
    {
        spawnedObject.transform.position = m_Hits[0].pose.position;
    }
    if(Input.GetTouch(0).phase == TouchPhase.Ended)
    {
        spawnedObject = null;
     }
}
```

10\. Lastly, we need to create a method that will take in a location and instantiate our Prefab. Add the following method below your Update method:

```csharp
private void SpawnPrefab(Vector3 spawnPosition)
{
    spawnedObject = Instantiate(spawnablePrefab, spawnPosition, Quaternion.identity);
}
```

11\. Save your script and head back to Unity (**Figure 11**).

![Figure 11: The completed SpawnableManager script](https://connect-mediagw.unity.com/h1/20210224/learn/images/d28b59c4-2751-4dbb-bdd2-441050fc13e0_image2.jpg)

12\. In your SpawnableManager script on your SpawnManager object, drag the **AR Session Origin** object from the Hierarchy into the **Raycast Manager** variable in the Inspector.

13\. Drag the SpawnableObject Prefab from the Project window into the **Spawnable Prefab** variable (**Figure 12**).

![Figure 12: The completed SpawnManager object](https://connect-mediagw.unity.com/h1/20210224/learn/images/17e16272-0b7d-4234-b8dc-3a1c17642c95_image9.jpg)

## Configuring Your Build Settings for Android

If you’re using Android, you must first change a few settings in order to build your app.

1\. Go to **Edit** > **Project Settings…** > **Player** > **Other Settings** and go to the Graphics APIs section. Remove **Vulkan API** from the list (**Figure 13**).

![Figure 13: Remove the Vulkan API so you are left with the OpenGLES3 API.](https://connect-mediagw.unity.com/h1/20210224/learn/images/3ac1343e-994d-4c3b-a16e-1bf42598db5d_image14.jpg)

2\. Set the Minimum API Level to **API Level 24** or higher (**Figure 14**).

![Figure 14: Set the Minimum API Level to API level 24 or higher.](https://connect-mediagw.unity.com/h1/20210224/learn/images/ee7fa6f4-67e1-460e-8a71-2e67670db099_image4.jpg)

3\. Open a web browser and go to [https://gradle.org/releases](https://gradle.org/releases/)

4\. Download **Gradle v5.6.4** and unzip it into a location of your choosing.

5\. Go to **Edit** > **Preferences…** > **External Tools** and uncheck **Gradle Installed with Unity**, then set the Gradle location to the location where you unzipped the download (**Figure 15**).

![Figure 15: Uncheck Gradle Installed with Unity and set the Gradle location.](https://connect-mediagw.unity.com/h1/20210224/learn/images/af62bca2-4734-422e-ab06-cc774f1b38e6_image3.jpg)

6\. Now we just need to alter some Gradle files located in Assets\\Plugins\\Android inside your Project folder.

7\. Go to [https://developers.google.com/ar/develop/unity/android-11-build](https://developers.google.com/ar/develop/unity/android-11-build) and follow the directions in the Unity 2019.3 and 2019.4 section.

8\. Build and launch your project on your device.

## Conclusion

You can now spawn a single object and adjust its position, and spawn multiple objects in your Scene. You are well on your way to locking in the basics of mobile AR interaction.
