How to assign 'Entity' to array element?

Heyy guys facing one issue about assigning entity to array element… please help.

This peace of code is working:

But this is NOT :frowning:

this.road_unit is ‘Entity’ which I’m assigning from editor window.

Screenshot (74)

this.road_units are array element:

Screenshot (75)

And here is error i’m getting in browser:

Please Help, Many thanks in Advance

Why not use Array.prototype.push ?

this.road_units.push(this.road_unit.clone());

It should work too.

1 Like

Thanks for your reply… @scarlex

But again same, this is also not working… and same error is there :frowning:

Could you share your project link or create a minimal demo for your issue ?

yes here it is: https://playcanvas.com/project/529432/overview/testproject please have a look once. Many Thanks :slight_smile:

This line:

posZ = this.road_units[this.road_units.length - 2].getPosition().z - 8;

Change 2 to 1. Because when this.road_units.length equals 1, the index will be -1, that’s out of range.

Ohh… i forgot to change that… was just experimenting some other logic…

Can you please check the line no 42 once:

I’m trying to do spawn pickups again on the road_unit once its position gets reset to back… Actually this is an actual issue for which i’m looking for. (i.e pickups not re-spawning after road_unit gets reset its position.)

I don’t know your game logic. :disappointed_relieved:
In spawnPickUps, you pass unit and add children for it. The result is unit.children will become larger and larger. Why you do this, it seems useless…

In SpawPickup function i’m passing the one road_unit and this function will consider the road_unit as parent and add pick up objects as a child…

As you can see here, the same is working (when initially I’m adding pickups on road_unit) line no 68:

04 AM

I’m also wondering why the same function works at one place but not at other :frowning:

So my thought on this was: we can not assign ‘Entity’ to the Array element in java-script (or in PlayCanvas).

By this i wrote heading: How to assign ‘Entity’ to array element ?

Anyways is there any other workaround can you suggest me here ?

Thanks a lot for your time