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.
