Hello,
The version I’m using is 2.4.3.
Need help to change the material of a sprite in script.
I know how to change it in cocos creator app (drag and drop) but can we do that using script during the game.
Lets say I have 2 material MaterialA and MaterialB. initially the sprite this.player
has material as MaterialA I want to change it to MaterialB during the game.
I got the below as solution
Define this property in your script and assign material in editor
MaterialB: {
default: null, type: cc.Material },
After that add this in script where you need to change
this.node.getChildByName(“Name_Of_The_node”).getComponent(cc.Sprite).setMaterial(0,this.MaterialB)
This will change material from previous material to MaterialB.
If there is a better way to do it do mention it.
Thank you.
HI,
Did you check this-https://docs.cocos.com/creator/manual/en/render/material.html#set-material-in-script
1 Like
Thanks for the solution
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.