Copying items in JavaScript

How can we copy items in JS? We are trying to duplicate a wall, and it won’t work.

Also, we have paid accounts. However, a couple of my students are trying to post in the forum, but can’t create accounts because our IP address limit has been reached. I am not sure why my students can’t create posts in here if we have a paid account.

Hi @Kelly_Nickell,

we just raised the sign-up limit for IPs, so the entire class should be able to sign up now :slight_smile:

To copy an item in JS, you can use the script in the space I shared below. The example demonstrates how to copy an item using JS and CoBlocks.

Please note that the JS method is still internal and may be deprecated later (your code editor won’t recognize it, but it will work). We’re currently working on making our API much more accessible and easy to read!


Press the Remix Button to copy this space and to see the code inside!

1 Like

I saw the guide well. By the way, the first box should move to 0,0,0 but it will not move. Is it a bug?
thanks…a lot
from korea 3DKIDS

JONGWON SEO ^^

Hi @Jongwon_Seo_3DKIDS,

The first box does not move because Physics are enabled on the item. If you want to move physics items, please use blocks from the Physics category in CoBlocks.

To fix this issue, you can remove physics from the cube:

Hope that helps!

1 Like

Hi, @Stefan,
Unfortunately the Remix Button is not available under the link you’ve provided (at least for me, also after I’ve login to CoSpaces). So, I could only see the “play” mode, no code.
Could you please show the example of JS code directly here?
Regards

Hi @likavot,

Thanks for pointing out the issue! I’ve updated the space to allow remixing again, feel free to make a copy :slight_smile:

The space has two scenes (one CoBlocks example, one TypeScript (JavaScript) example). Below you can find the code for the scripting example:

// Get the item you want to copy (here it is a cube called "exampleCube")
const cube = Scene.getItem('exampleCube')

let newPosition = new Vector3(0, 0, 0)

// Every second, create a copy of the cube and place the copy 0.5 units away along the Y-axis of the last position
Time.scheduleRepeating(() => {
    newPosition = newPosition.add(new Vector3(0, 0.5, 0))

    const cubeCopy = cube.copy()
    cubeCopy.transform.position = newPosition
}, 1)