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

The `ref` and `out` คำสำคัญใน C# ถูกใช้เมื่อส่งผ่านการอ้างอิงไปยังตัวแปรหรือโครงสร้างไปยังวิธีการ.

ใน TypeScript, คำสำคัญเหล่านี้ไม่มีให้ใช้, แต่ใน ZEPETOScript, คุณสามารถทำให้ฟังก์ชันการทำงานที่คล้ายกันได้โดยใช้ `$ref` และ `$unref`.

| ฟังก์ชัน          | คำอธิบาย                                      |
| ----------------- | --------------------------------------------- |
| $ref(x?\:T):$Ref  | สร้างการอ้างอิงสำหรับพารามิเตอร์              |
| $unref(x:$Ref)\:T | ปล่อยการอ้างอิงของพารามิเตอร์และคืนค่าต้นฉบับ |



สำหรับตัวอย่าง, มาตั้งค่า Scene ดังนี้:

- เพิ่มวัตถุ 3D, ลูกบาศก์, ลงในฉาก.

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



- ใน Inspector สำหรับลูกบาศก์ที่เพิ่มเข้ามา ให้คลิก "Add Component" และเพิ่ม Rigidbody.

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



- เพิ่ม GameObject เปล่าในฉาก.
- เขียนสคริปต์ RefSample ด้านล่างและเพิ่มมันไปยัง GameObject ที่สร้างในขั้นตอนที่ 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}`);
        }
    }
}
```



- สร้าง ZEPETOScript อีกอันและเขียน TestScript ด้านล่าง.

```typescript
import { ZepetoScriptBehaviour } from 'ZEPETO.Script'
 
export default class TestScript extends ZepetoScriptBehaviour {
 
    DoTest() {   
        console.log(`TestScript's DoTest() ถูกเรียกใช้งาน.`);
    }
}
```



- ลากและเชื่อมต่อ TestScript กับ Inspector ของ Cube.
- คลิกที่ปุ่ม \[▶︎(เล่น)] เพื่อสังเกตผลลัพธ์.
- คุณสามารถตรวจสอบความสำเร็จของการอ้างอิงวัตถุ, คอมโพเนนต์ Rigidbody, และคอมโพเนนต์ ZEPETOScript ในหน้าต่างบันทึกคอนโซล.

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

