> For the complete documentation index, see [llms.txt](https://xreal.gitbook.io/nrsdk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xreal.gitbook.io/nrsdk/v1.10.2/development/plane-detection-tutorial/add-gems.md).

# Add gems

Now that the player can control an entity in the scene, give the player a destination to drive towards.

![](https://1363773068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL6MonFe6Phoj4t5Xi1d0%2Fuploads%2Fq6R6QeetCHSRGBeCF1iK%2Fimage.png?alt=media\&token=a4d73ad5-42c0-4ae5-b3ef-34ee04a52dae)

### Create `GemSpawner`

1. In **Assets/Scripts,** create a new script named `GemSpawner.` This script defines how the gems are generated randomly on the current plane.

```csharp
using NRKernal;
using NRKernal.NRExamples;
using UnityEngine;

public class GemSpawner : MonoBehaviour
{
    public GemBehaviour Gem;
    public GameObject GemPrefab;
    public ReticleBehaviour Reticle;

    public static Vector3 RandomInTriangle(Vector3 v1, Vector3 v2)
    {
        float u = Random.Range(0.0f, 1.0f);
        float v = Random.Range(0.0f, 1.0f);
        if (v + u > 1)
        {
            v = 1 - v;
            u = 1 - u;
        }

        return (v1 * u) + (v2 * v);
    }

    public Vector3 FindRandomLocation(GameObject plane)
    {
        // Select random triangle in Mesh
        var mesh = plane.GetComponent<PolygonPlaneVisualizer>().m_PlaneMesh;
        var triangles = mesh.triangles;
        var triangle = triangles[(int) Random.Range(0, triangles.Length - 1)] / 3 * 3;
        var vertices = mesh.vertices;
        var randomInTriangle = RandomInTriangle(vertices[triangle], vertices[triangle + 1]);
        var randomPoint = plane.transform.TransformPoint(randomInTriangle);
        randomPoint.y = Reticle.CurrentPlane.GetComponent<NRTrackableBehaviour>().Trackable.GetCenterPose().position.y;
        return randomPoint;
    }

    public void SpawnGem(GameObject plane)
    {
        var gemClone = Instantiate(GemPrefab);
        gemClone.transform.position = FindRandomLocation(plane);

        Gem = gemClone.GetComponent<GemBehaviour>();
    }

    private void Update()
    {
        if (Reticle.CurrentPlane != null)
        {
            if (Gem == null)
            {
                SpawnGem(Reticle.CurrentPlane);
            }
        }
    }
}
```

### Add the game element <a href="#add-the-game-element" id="add-the-game-element"></a>

1. Create a new empty `GameObject` in the Hierarchy.
2. Rename it to `Gem Spawner`.
3. Select the object you created. In the **Hierarchy** pane, click **Add Component** to add the `GemSpawner` component to it.
4. Set up `GemSpawner`'s dependencies by clicking on the chooser for the following field:
   * **Gem Prefab**: In **Assets**, select **Gem Prefab**.
   * **Reticle:** In **Scene,** select **Reticle Prefab.**

### Create `GemBehaviour`

1. Create a script named `GemBehaviour` in PlaneDetectionStarterPackage folder. You needn't add anything else to this script.
2. Associate `GemBehaviour`  with **Gem Prefab** by editing **Gem Prefab,** clicking **Add Component,** and choose `GemBehaviour` you've created.

### Modify Car Behaviour

In `CarBehaviour` you've previously created, add the following event function which defines the action when the car hits the gem.&#x20;

```csharp
    private void OnTriggerEnter(Collider other)
    {
        var Gem = other.GetComponent<GemBehaviour>();
        if (Gem != null)
        {
            Destroy(other.gameObject);
        }
    }
```

### Test the game <a href="#test-the-game" id="test-the-game"></a>

1. Click **File > Build** to test your changes. 2, After you create a car, a gem should spawn.
2. Drive your car to the gem.
3. A new one will appear at a random location.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://xreal.gitbook.io/nrsdk/v1.10.2/development/plane-detection-tutorial/add-gems.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
