Same script multiple times on single entity


Loving PlayCanvas so far! Very powerful and intuitive engine.

I do have a request/idea, however. It would be great if scripts could be added multiple times to an entity and were completely new object instances every time. That would allow for super flexible scripts.

For example, let’s say I create a SwapMaterial script. This script accepts 2 script attributes: Model and Material. When I place this script on a UI sprite, the UI sprite will now listen for clicks. When it’s clicked, it will update the specified model with the specified material. Pretty simple! And works great.

Buuut…what if there are multiple models that I want updated when this sprite is clicked? Ideally, I could add the script a second time, specify the Model and Material in the second instance’s script attributes, and viola–another SwapMaterial instance is active.

But since the UI currently only allows a script to be attached once, this isn’t possible.

It seems like pretty basic object instancing, so I feel like it wouldn’t be that difficult to add to the editor–but then I also don’t know the inner workings of how the engine is set up. If this was possible, it would greatly increase the ability for people to create libraries of re-usable scripts within PlayCanvas.

Any thoughts, ideas? Is this something that could be added in the future?


Hello, not sure myself and i think some of the guys of playcanvas crew will be more precise, but i think you can use tags to make that, i mean you give the same tag to the sprites you want to update and so refer to the tag in the script and all the sprites will be updated in 1 go… but to be sure wait for a reply of the crew.

Thanks ayrin! It would be kind of similar to use the set up you described, but not nearly as flexible. Most notably, it wouldn’t out-of-the-box allow you to specify different model/material combinations for every listener.

On engine level we didn’t design it to allow mutliple scripts of same type. But I believe we could look into some solution to keep current workflow working, and to allow getting script by index if there are many.

We’ve noted that this was a useful sometimes too.

If you make the script with an attribute string you can put it in several entities define the attribute script with the name of the texture for every sprite, when click you change a variable that’s checked from all scripts and the game is done. If you need multiple textures you just need an array instead a string and multiple values when click.

Great! I feel like being able to re-use the same script on a single entity would open up a world of functionality. I’m sure you folks have pretty full plates and there are a lot of requests on the table…any idea if this might actually make it in to the core in the future (near/far/etc?).

On a related note, is there any way for third parties (i.e. me) to contribute to the PlayCanvas editor? I see the engine on Github, but I’m under the impression that’s not the same as the editor itself.

We have been thinking about adding support for 3rd party plugins to the editor but at this stage, no there isn’t unfortunately. Something like multiple same scripts on the entity will need to be supported in the engine first though before being exposed in the editor.