- Home /
Changing sprite at runtime causes it to stop resizing
So I'm changing the sprite on an Image component at runtime and this causes it to stop resizing with the Image's RectTransform and just stays a fixed size. The code that changes the sprite (in Start()):
public Sprite mySprite;
GetComponent<Image>().sprite = mySprite
If I comment it out the image resizes fine so I know this is the problem.
Also, this exact code worked fine in Unity 4, but ever since I updated it won't work.
Answer by highpockets · May 28, 2019 at 07:43 AM
You need to change your code to the following:
public Sprite mySprite;
void Start()
{
mySprite = GetComponent<Image>().sprite;
}
The way you have it now, sets the sprite in the component to mySprite, you want to set mySprite to have the sprite from the component instead
This does the reverse of what I want though. I want to change the sprite in the Image component to a different sprite at runtime.
Your answer
Follow this Question
Related Questions
How to stop a sliced image from shrinking with the container? 0 Answers
How do importing image when game is running (Run Time) ???? 0 Answers
Help with changing spririterenderer's sprite 2 Answers
Display Raw Images at runtime in unity 4.6 0 Answers
how to set Image that converted from Base64 string to Sprite 1 Answer