I need to able to edit a sprite’s texture in the game I’m making in order to show damage, this was fairly easy using quads, however I can’t seem to find a way to do this using sprites, sprite.texture is read-only and I can’t seem to get Sprite.Create to work properly (even though the texture to use in the sprite is generated without any problem, the generated sprite is always a solid color no mater what I do). Any ideas?
It seems to me that you cannot change the texture, you should create a whole new Sprite for the SpriteRenderer, like this for example :
SpriteRenderer sr = GetComponent<SpriteRenderer>();
sr.sprite = Sprite.Create(yourTexture2D, new Rect(0, 0, yourTexture2D.width, yourTexture2D.height), new Vector2(0, 0), pixelsToUnitfloat);
Check Sprite.Create doc
In the import inspector of the texture, select advanced and tick Read/Write.
That will allow you to modify your texture.
But think what you are after is more likely to change a texture.
So you need to have an array of texture:
public Sprite[] textures;
SpriteRenderer sp;
void Start()
{
sp = GetComponent<SpriteRenderer>();
sp.sprite = textures[0];
}
void Damage(int damage)
{
health -= damage;
if(health > 80 )
sp.sprite= textures[0];
else if(health <= 80 && health > 50)
sp.sprite = textures[1];
// And so on
}
isn’t that working?