---
title: $ref & $unref
slug: world-sdk-guide/dollarref-and-dollarunref
docTags: 
createdAt: 2024-09-02T08:34:13.135Z
---

The `ref` and `out` keywords in C# are used when passing references to variables or structures to methods.

In TypeScript, these keywords are not available, but in ZEPETOScript, you can achieve similar functionality using `$ref` and `$unref`.

| Functions         | Description                                                          |
| ----------------- | -------------------------------------------------------------------- |
| $ref(x?\:T):$Ref  | Creates a reference for a parameter                                  |
| $unref(x:$Ref)\:T | Releases the reference of a parameter and returns the original value |



For the example, let's set up a Scene as follows:

- Add a 3D object, Cube, to the Scene.

::Image[]{src="https://api.archbee.com/api/optimize/fCt3n1oCa8rgNJ8fw9I2N-NdO0i7dLaDpFX1zUPm-6p-20240904-102412.png" size="52" width="592" height="742" position="center" showCaption="false"}



- In the Inspector for the added Cube, click "Add Component" and add a Rigidbody.

![](https://api.archbee.com/api/optimize/fCt3n1oCa8rgNJ8fw9I2N-px7dpEvf7j1af6Dfb2oBc-20240904-102413.png)



- Add an Empty GameObject to the Scene.
- Write the RefSample script below and add it to the GameObject created in step 3.

```typescript
import { GameObject, Rigidbody } from 'UnityEngine'
import { ZepetoScriptBehaviour } from 'ZEPETO.Script'
import TestScript from './TestScript';
 
export default class RefSample extends ZepetoScriptBehaviour {
 
    Start() {   
        // Get the GameObject named "Cube"
        const testObject = GameObject.Find("Cube");
         
        // Get the Rigidbody Component from the "Cube" Object
        const testComponent = GameObject.Find("Cube").GetComponent<Rigidbody>();
         
        // Get the TestScript Component from the "Cube" Object
        const testScript = GameObject.Find("Cube").GetComponent<TestScript>();
 
        // Create references to the components and object
        let tempObj = $ref(testObject);
        let tempComponent = $ref(testComponent);
        let tempScript = $ref(testScript);
 
        // Check if the Rigidbody component is not null
        if(tempComponent != null) {
            // Access the actual value of that reference
            let component = $unref(tempComponent);
            console.log(`Component Name : ${component}`);
        }
 
        // Check if the TestScript component is not null
        if(tempScript != null) {
            // Access the actual value of that reference
            let script = $unref(tempScript);
            script.DoTest();
        }
 
        // Check if the GameObject reference is not null
        if(tempObj != null) {
            // Access the actual value of that reference
            let objName = $unref(tempObj);
            console.log(`tempObj Name : ${objName.name}`);
        }
    }
}
```



- Create another ZEPETOScript and write the TestScript below.

```typescript
import { ZepetoScriptBehaviour } from 'ZEPETO.Script'
 
export default class TestScript extends ZepetoScriptBehaviour {
 
    DoTest() {   
        console.log(`TestScript's DoTest() is executed.`);
    }
}
```



- Drag and connect TestScript to the Cube's Inspector.
- Click the \[▶︎(play)] button to observe the results.
- You can check the success of referencing objects, Rigidbody components, and ZEPETOScript components in the console log window.

![](https://api.archbee.com/api/optimize/fCt3n1oCa8rgNJ8fw9I2N-HVCJXiBnwDumPbg9gfyim-20240904-102413.png)

