- Home /
Change sprite of UI image and then set time.timescale = 0;
I'm setting up a UI that floats on the screen after launching a level. The image changes to the appropriate image for the level and then the game freezes for a couple of seconds.
All good, but for some reason the sprites don't change.
This code is a component on the Image:
Image image;
public Sprite level1;
public Sprite level2;
private void Awake()
{
if (PlayerPrefs.GetInt("CurrentLevel") == 1)
{
image.sprite = level1;
Debug.Log("Current Level: " + PlayerPrefs.GetInt("CurrentLevel"));
}
if (PlayerPrefs.GetInt("CurrentLevel") == 2)
{
image.sprite = level2;
Debug.Log("Current Level: " + PlayerPrefs.GetInt("CurrentLevel"));
}
}
private void Start()
{
Time.timeScale = 0;
}
Does anyone have a clue why this doesn't work? I don't think the timeScale = 0; is the issue actually since this function is called after Awake().
Answer by Mr_parkour10021 · Feb 04, 2019 at 11:05 PM
@Tjoeker try to change image.sprite
to image.overrideSprite
because image.sprite
returns the sprite on the image and I don't think you can set the sprite like that. On what line does it say that you haven't set the object reference to an object?
Hey, thanks for the answer.
I have tried what you said, but it didn't fix it unfortunately.
The error is thrown at that specific line. Also after I changed it to your suggestion. image.(override)sprite = levelX; with X being the level number.
the line where you are declaring an Image variable, you are declaring it as private and I don't see any reference to it in the code. if you want it to still be private initialize the image variable in the Awake() method otherwise make the variable public and set it in the inspector. basically, image = null so when you're trying to get a reference to it. it's throwing an error because you are trying to modify a value that does not exist.
How could I have ignored that for so long? Thanks mate! ;)
Your answer
Follow this Question
Related Questions
Sprite import problem? 1 Answer
PNG Sprite scaling issues 1 Answer
Change UI GUI Image Sprite at runtime .. Unity 5 .. js 1 Answer
Why is Sprite a null? 0 Answers
How would you mask a UI canvas like you would mask a sprite? 1 Answer