Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tjoeker · Feb 01, 2019 at 04:34 PM · uisprite

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().

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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?

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tjoeker · Feb 05, 2019 at 09:42 AM 0
Share

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.

avatar image Mr_parkour10021 Tjoeker · Feb 05, 2019 at 08:55 PM 0
Share

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.

avatar image Tjoeker Mr_parkour10021 · Feb 06, 2019 at 09:54 AM 0
Share

How could I have ignored that for so long? Thanks mate! ;)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

191 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges