- Home /
Button's sprite swap works fine but it doesn't change the related image's source image! why?
Hi, i'm trying to get access to the current sprite of the button's image component, but there is something that confuses me and it's that :
when i set the button interactable parameter to false and then the sprite swap does it's job and changes the sprite of the button's image to Disabled Sprite state, the thing that it happens is that i can see that the button's sprite changes in game but when i'm accessing the source image of the image component i still get the last one not the current image that it's being shown in game.
so i think there is something related to target graphic of the button that i don't know and also is there a way to get access to the current sprite via Image component?
in this image button is interactable and it shows the white sprite:
here button in not interactable and as you can see sprite that is shown is red but the source image still has white sprite in it!!!
Answer by evskii · Nov 08, 2021 at 03:04 PM
There is a lot of information missing here so it might be hard to help. Maybe a few screenshots would help!
From what I can understand you want to get access to the sprite that is in the Image component of a button. Are you trying to access this through code or in the inspector?
Oh I understand your issue now. Since you have the Transition set to Sprite Swap it'll use those sprites to basically override/render above the Image component on the button. Unity used to have some easy ways to see which Sprite was being used but a lot of it is deprecated. (https://docs.unity3d.com/530/Documentation/ScriptReference/UI.Selectable-spriteState.html)
It seems impossible as of right now to access what Sprite is currently being used by the button. What I would suggest if you want to keep track of something like that is a script that switches the Sprites in the Image component itself rather than using the Sprite Swap transition, or even just use the Color Tint transition if you are just using Red and White etc.
You can access the sprites that are referenced in the inspector using SpriteState, but there is no parameter for finding which sprite is actively being used as far as I know.
true, i've already used the SpriteState but i thought Image component it's responsible for visual state of the button so i can easily get access to the sprite that is actively showing but apparently not, which i really do not get why would unity not allow or create such a access via Image component?!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Trouble detecting button release with Input.GetButtonUp() 1 Answer
Animation not working 1 Answer