Sprite change of an object doesn't work.. how to fix?

Hi, I’m new to Unity and this forum, so I apologize if this is the wrong section.

I’m trying to change the sprite of a button object when an action is triggered by the user, but the problem is that the swap of the sprite doesn’t happen at all.

This is the tricky part of the code I’m using, I avoid to load every line because that could be confusing since I’m not a programmer at all, so the code would not be perfect.

  • “mask” is an item that the player has in their inventory, with a name string parameter between the other things;
  • I use the name of the mask to take from Resources the sprite with the same name (it exists);
  • “MaskMenuChoice” is a tag. Four buttons are tagged with this tag, but from the debug i see that floatingButton1 takes the last button of them, so I expect it to have a sprite change.
//get correct sprite
Sprite iconCorrectSprite = Resources.Load(mask.name) as Sprite;
                        Debug.Log(mask.name);
                        Debug.Log(iconCorrectSprite.name);
                        // get button's sprite
                        floatingButton1 = GameObject.FindGameObjectWithTag("MaskMenuChoice");
                        Debug.Log(floatingButton1.name);
                        Sprite floatingButton1_sprite = floatingButton1.GetComponent<SpriteRenderer>().sprite;
                        Debug.Log(floatingButton1_sprite.name);
//swipe sprites
                        floatingButton1_sprite = iconCorrectSprite; //this is not working
                        Debug.Log("sostituizione avvenuta.");

I attached a picture with the Console output, and as you can see the 4 buttons (Floating Button 1, …, Floating Button 4) tagged as “MaskMenuChoice” are all active in Hierarchy.

Where is my fault? I really don’t understand since the debug gives me back nothing wrong, but sprite still doesn’t change.

I think it’s because of this:

Sprite floatingButton1_sprite = floatingButton1.GetComponent<SpriteRenderer>().sprite;
floatingButton1_sprite = iconCorrectSprite;

Try this instead:

var renderer = floatingButton1.GetComponent<SpriteRenderer>();
renderer.sprite = iconCorrectSprite;

Assign the new sprite directly to the SpriteRenderer.sprite property instead of getting the sprite value from the SpriteRenderer and assigning it to a local Sprite variable, then assigning a new value to the local Sprite variable.

The way you were trying to do it would only work if SpriteRenderer.sprite returned its sprite by reference, but that is not the default behavior for return values and there are a few good reasons they would not do it anyway. When assigning a new sprite directly to the SpriteRenderer.sprite it can be handled by a property, which allows the SpriteRenderer class appropriate control over exactly what happens during assignment. If it returned to your local variable a direct reference to its internal Sprite instance, then it loses that control. It would be like port forwarding, so some external connection could slide right through the firewall. Also, you would have to declare your local variable as a ref variable.

Thank you both, this really helped me to fix the error and understand better how it works.