> For the complete documentation index, see [llms.txt](https://danil-dukhovenko.gitbook.io/force/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://danil-dukhovenko.gitbook.io/force/force-editor/scripting/prefabs-using-scripts.md).

# Prefabs Using Scripts

Prefabs can be instantiated automatically or they can be instantiated using a script. In this example, we will instantiate prefabs using a script.

```csharp
public class PrefabExample : MonoScript {

	public GameObject prefab;

	// Runs when scene starts to play.
	public void OnStart() {
		GameObject redSquare = CreateGameObject(prefab);
		if(redSquare != null)
			Debug.Log("Prefab " + redSquare.name + " is instantiate!");
	}
}
```

The script example below has a single public variable, `prefab`, that is a reference to a **Prefab**. It creates an instance of that Prefab in the `OnStart()` method.

For make this example work you need firstly create a prefab. You can do this by go in to **Assets Browser**, **LMB -> Create -> Prefab**, and enter the name of it. In our case its **Red Circle**.

<figure><img src="https://2773422523-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuytHSSE0uhny7g4jsNpY%2Fuploads%2FFAMWwZU1ddo3g5ZhrUZh%2Fimage.png?alt=media&amp;token=b2d8cbbd-6e89-4b39-9aa6-ac4bd371e3d3" alt=""><figcaption><p>Creating new Prefab via window and seeing prefab Asset in Browser</p></figcaption></figure>

Or you can create a simple object in **Explorer** configure, name it **Red Square** it and click on it **LMB -> Create Prefab**. And its also appears in Browser. If prefab with the name already exist Force rewrite the Prefab asset with new version of it.

<figure><img src="https://2773422523-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuytHSSE0uhny7g4jsNpY%2Fuploads%2Fu73mizmbDKWOS63sNwf5%2Fimage.png?alt=media&amp;token=e2da9190-a0f2-43ab-a819-a50b3b5c28b3" alt=""><figcaption><p>Creating Prefabs using Explorer context menu</p></figcaption></figure>

Next, you will need to create a new object and attach the script component to it. Next, we insert our example into the script.

<figure><img src="https://2773422523-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuytHSSE0uhny7g4jsNpY%2Fuploads%2FncWiTNXJSdVMhHUj7VLT%2Fimage.png?alt=media&amp;token=8b99f199-7e79-41f2-834d-cef7bb10ad00" alt=""><figcaption><p>Game Object with PrefabExample.cs script</p></figcaption></figure>

Then drag prefab asset from **Assets Browser** to our field Prefab.

<figure><img src="https://2773422523-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuytHSSE0uhny7g4jsNpY%2Fuploads%2FqKjhrdvYXeX5mL2muJWI%2Fimage.png?alt=media&amp;token=f14c41a4-b78c-41d9-b42b-39254f5bbb77" alt=""><figcaption><p>Attaching Prefab to script as field (reference)</p></figcaption></figure>

Then click play, and you can see the our prefab will successfully instantiated via our script code at position (0, 0, 0). Also you can that prefab display with different icon and text in **Inspector**.

<figure><img src="https://2773422523-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuytHSSE0uhny7g4jsNpY%2Fuploads%2F2aKMeNjdlvss576qJzd2%2Fimage.png?alt=media&amp;token=65f030c0-4448-4c96-94fc-d1799416f827" alt=""><figcaption><p>Testing the script</p></figcaption></figure>

Because this first example is very simple, it may not seem to provide any advantage over just placing a Prefab into the Scene yourself. However, being able to instantiate Prefabs using code provides you with powerful abilities to dynamically create complex configurations of GameObjects while your game or app is running.
