CREATE YOUR WORLD
Scripting

GameObjects and Components

13min

In the world, it is very common to create objects or modify or add specific components to objects.

ZEPETOScript supports the same interface as that used in the Unity script.

Basic Examples of GameObject and Component

Creating and removing game objects:

GameObject


To get GameObjects components or add new ones, use GetComponent or AddComponent.

Component




Check the full code example for the GameObject Component.

Here's an example that covers the creation and property modification of GameObject, addition of components, and destruction.

GameObjectSample




Utilizing Find

The Find related methods also support the same interface style in ZEPETOScript as in Unity script.

Methods

Description

GameObject.Find()

- It finds and returns an active GameObject object based on the name in the current Scene. - Returns null if not found.

GameObject.FindGameObjectWithTag()

- It finds and returns an active GameObject object based on the tag in the current Scene. - Finds and returns the first object matching the specified tag among the active objects, returns null if not found.

GameObject.FindGameObjectsWithTag()

- Finds all active GameObject objects with a specific tag in the current Scene and returns them as an array. - Returns an empty array if none are found.

For the example, set up the Scene as follows:

  • Add several 3D objects and specify all their Tags as 3D.
  • Create an Empty Object to attach the script to, and rename it to FindSample.
Document image




TypeScript




  • The inspector is empty when you add a script in the FindSample object.
Document image




  • Press the Play button to run it, and you can confirm in the Inspector window that each object has been assigned and check the success of finding objects through the console window.
Document image


Creating Prefabs

Let's explore how to create prefabs during runtime.

  • First, add a 3D object > Cube to the Scene.
Document image




  • Press Add Component in the inspector of Cube, and add Rigidbody.
Document image




  • Then drag the Cube to the project area to make it a prefab.
Document image




  • Since we will utilize the Prefab, delete the Cube in the Scene.

    Document image
    

    

  • Please write the script below.
TypeScript

  • Then return to the Unity editor, drag the prefab to the script inspector to add it.
Document image




  • Press the play button to check that a 3D Object is created and falls from above every second.
Document image


Click the links below to learn more about the Unity GameObjects and Components offered by ZEPETOScript.





Updated 10 Oct 2024
Doc contributor
Did this page help you?