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 // gameobject create const tempobj = new gameobject(); const obj = object instantiate(tempobj); // gameobject destroy object destroy(obj); to get gameobjects components or add new ones, use getcomponent or addcomponent component // getcomponent with generic const mytransform = this getcomponent\<transform>(); // addcomponent with generic const animator = this gameobject addcomponent\<animator>(); 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 import { gameobject, object, transform, animator, vector3, waitforseconds } from 'unityengine'; import { zepetoscriptbehaviour } from 'zepeto script'; export default class gameobjectsample extends zepetoscriptbehaviour { private testobject gameobject; start() { // create an empty gameobject const tempobj = new gameobject(); this testobject = object instantiate(tempobj) as gameobject; // change the object's name this testobject name = "testobject"; // change the object's transform information this testobject transform position = new vector3(3,3,3); // attach a component to the object this testobject gameobject addcomponent\<animator>(); // example of using getcomponent const animator = this testobject getcomponent\<animator>(); if(animator != null) { console log("getcomponent success"); } // destroy the object after 5 seconds using a coroutine this startcoroutine(this destroyobject()); } destroyobject() { yield new waitforseconds(5); // destroy the gameobject object destroy(this testobject); } } 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 typescript import { zepetoscriptbehaviour } from 'zepeto script'; import { gameobject, transform } from 'unityengine'; export default class findsample extends zepetoscriptbehaviour { public tempobj1 gameobject; public tempobj2 gameobject; public tempobj3 gameobject\[]; start() { // finds the target gameobject with the specified name // if multiple gameobjects have the same name, the first one found will be returned this tempobj1 = gameobject find("cube"); if(this tempobj1 != null) { console log("gameobject find success"); } // finds the target gameobject with the specified tag // if multiple gameobjects have the same tag, the first one found will be returned this tempobj2 = gameobject findgameobjectwithtag("3d"); if(this tempobj2 != null) { console log("findgameobjectwithtag success"); } // returns an array of gameobjects that have the specified tag this tempobj3 = gameobject findgameobjectswithtag("3d"); if(this tempobj3 != null) { console log("findgameobjectswithtag success"); } } } the inspector is empty when you add a script in the findsample object 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 creating prefabs let's explore how to create prefabs during runtime first, add a 3d object > cube to the scene press add component in the inspector of cube, and add rigidbody then drag the cube to the project area to make it a prefab since we will utilize the prefab, delete the cube in the scene please write the script below typescript import { gameobject, object, vector3, waitforseconds } from 'unityengine'; import { zepetoscriptbehaviour } from 'zepeto script'; export default class clonesample extends zepetoscriptbehaviour { public cloneprefab gameobject; start() { this startcoroutine(this doroutine()); } doroutine() { while(true) { yield null; // create a clone const clone = object instantiate(this cloneprefab) as gameobject; // set the height of the clone object clone transform position = new vector3(0, 10, 0); // destroy it after 5 seconds of creation gameobject destroy(clone, 5); yield new waitforseconds(1); } } } then return to the unity editor, drag the prefab to the script inspector to add it press the play button to check that a 3d object is created and falls from above every second click the links below to learn more about the unity gameobjects and components offered by zepetoscript 📘 creating gameobjects https //docs unity3d com/2020 3/documentation/scriptreference/object instantiate html https //docs unity3d com/2020 3/documentation/scriptreference/object instantiate html 📘 destroying gameobjects https //docs unity3d com/2020 3/documentation/scriptreference/object destroy html https //docs unity3d com/2020 3/documentation/scriptreference/object destroy html 📘 using components https //docs unity3d com/kr/current/manual/usingcomponents html https //docs unity3d com/kr/current/manual/usingcomponents html