Studio GuideWorld SDK Guide
Log In

$ref & $unref

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.

$ref(x?:T):$RefCreates a reference for a parameter
$unref(x:$Ref):TReleases the reference of a parameter and returns the original value

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

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

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

  1. Add an Empty GameObject to the Scene.
  2. Write the RefSample script below and add it to the GameObject created in step 3.
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);
        // 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 : ${}`);

  1. Create another ZEPETOScript and write the TestScript below.
import { ZepetoScriptBehaviour } from 'ZEPETO.Script'
export default class TestScript extends ZepetoScriptBehaviour {
    DoTest() {   
        console.log(`TestScript's DoTest() is executed.`);

  1. Drag and connect TestScript to the Cube's Inspector.
  2. 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.