Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
6
Question by tsx5000 · Feb 05, 2014 at 12:21 PM · texturespritespriterenderer

Change sprite texture through script

I have a game object with a sprite renderer component. The sprite renderer has not been assigned any sprite through the Inspector. I have two textures (say img1 and img2) in my Resources folder. I need to assign one of these textures as my sprite textures during run time. This is what I tried but doesn't seem to work :

 gameObject.GetComponent<SpriteRenderer> ().sprite = (Sprite)Resources.Load ("img1");

Any suggestions on how can this be done ?

Comment
Add comment · Show 1
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 Ashish Dwivedi · Feb 05, 2014 at 12:34 PM 0
Share

Check your folder name and path, that might be wrong.

4 Replies

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

Answer by tsx5000 · Feb 06, 2014 at 09:31 AM

SOLVED !

A small modification made the difference !

 gameObject.GetComponent<SpriteRenderer> ().sprite = Resources.Load("img1", typeof(Sprite)) as Sprite;
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 ThorShaker · May 13, 2014 at 08:01 PM 0
Share

Thank you for posting the solution, it helped me sort out my problem :)

avatar image lerio · Oct 27, 2015 at 03:54 PM 0
Share

Hello, have you tried simply writing:

gameObject.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("img1");

?

avatar image Gillissie · Mar 22, 2018 at 10:59 PM 1
Share

This is a very bad solution. 1. Putting Sprites in the Resources folder prevents them from being packed onto an atlas. 2. Calling GetComponent() unnecessarily on anything is slow and bad.

avatar image
5

Answer by Ashish Dwivedi · Feb 05, 2014 at 12:31 PM

You can declare two public Sprite variable and assign in inspector.

 public Sprite img1 , img2;
 
 void SetSprite()
 {
     gameObject.GetComponent<SpriteRenderer>().sprite = img1;
 }
 void SetSprite2()
 {
     gameObject.GetComponent<SpriteRenderer>().sprite = img2;
 }
Comment
Add comment · Show 5 · 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 tsx5000 · Feb 06, 2014 at 06:42 AM 0
Share

can you suggest a way to do it without using public variables ?

avatar image acocalypso · Apr 19, 2014 at 01:20 PM 0
Share

hey i tried that solution, my current sprite dissapears but the new one doesn't load. any ideas?

 void SetSprite()
 {
     gameObject.GetComponent<SpriteRenderer> ().sprite = Resources.Load("Goku_erw", typeof(Sprite)) as Sprite;
 }

 void Update()
 {
 if (manager.curScore == 1) 
 {
     SetSprite();
 }
 }
avatar image MutantJedi · Feb 03, 2015 at 05:49 AM 0
Share

Same, I'm getting a blank too. Were you able to solve that?

avatar image tsx5000 · Feb 03, 2015 at 06:58 AM 0
Share

I am not facing any such problems. Please check if your image path is correct.

avatar image jms_8 · Aug 10, 2015 at 11:05 PM 0
Share

I also could not get it to work initially. For me in C#, the following worked:

 gameObject.GetComponent ().sprite = (Sprite)Resources.Load ("img1", typeof(Sprite)); 
avatar image
2

Answer by DrMike91 · Sep 30, 2016 at 05:04 PM

The sprites need to be in a "Resources" folder under Assets in the Project tab. I had to create a fodler called Resources and move my sprites their for this code to work.

Comment
Add comment · 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
1

Answer by Gillissie · Mar 22, 2018 at 10:57 PM

Hate to say it, but these are horrible solutions. The best suggestion here is to create two Sprite inspector variables and use the one you need at any given time, however, you should also make a SpriteRenderer inspector variable instead of calling GetComponent every time you need to set it.

The worst thing you could do here is use Resources.Load(), because if you put Sprites in the Resources folder, they aren't packed to an atlas, which ruins the optimization of sprites.

Comment
Add comment · Show 11 · 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 vovx · Mar 27, 2018 at 04:52 PM 0
Share

Yes your suggestions are better. Do you know how to get spriterenderer component without GetComponent?

avatar image Gillissie vovx · Mar 27, 2018 at 04:54 PM 0
Share

Yes, just simply make an inspector property just like anything else.

public SpriteRenderer spriteRenderer;

avatar image CyberAngel · Apr 27, 2021 at 10:09 PM 0
Share

I am going to have to disagree with you @Gillisie, what if there are hundreds or thousands of sprites, you are saying that the user should waste memory holding onto sprite information that may or may not get used.

Now, while GetComponent() should be used in places like the Awake or Start method, it is safe to do that, rather than in areas that are deemed to be more critical.

As for the resources.load, you can create an atlas or sprite sheet, give every sprite a meaningful name, and reference them via the Resouces.load without any issues.

It is actually a better solution, than what you proposed here.

avatar image Gillissie · Apr 28, 2021 at 01:27 AM 0
Share

@CyberAngel the main issue I have with the original solutions is calling GetComponent(). Calling GetComponent() on anything more than once is wasteful. Sure, sometimes it's ok to do it once in Awake() and store the reference in a variable.

But that has nothing to do with hundreds or thousands of sprites. I'm not sure what you're referring to there.

However, as I mentioned in my original comment, using Resources.Load() on a sprite is a really bad idea because it prevents it from being packed into an atlas. If you want to reference sprites by name... 1. Create a SpriteAtlas asset. 2. Link the sprites you want to include on that atlas. I find it easiest to link a folder and just put all the sprites in that folder. You can even organize your sprites in subfolders and they will all be included in the atlas. 3. Link the SpriteAtlas asset to whatever MonoBehaviour needs access to its sprites, using a public inspector property. 4. Call SpriteAtlas.GetSprite() to get a sprite by name from the linked atlas.

Now you have access to all of your sprites on that atlas by name, and they are actually packed into an atlas during builds.

avatar image CyberAngel · Apr 28, 2021 at 01:29 AM 0
Share

Sorry you are wrong about the sprite being packed into an Atlas, I do it all the time.

avatar image Gillissie · Apr 28, 2021 at 01:32 AM 0
Share

Have you actually looked at your build report to see what assets are in there? This is how I realized that sprites in the Resources folder are stored as separate assets. It may appear to be working perfectly fine, but you're wasting memory and draw calls.

Show more comments

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

26 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

Related Questions

Sprite Renderer is wrapping bottom pixel when it shouldn't be 1 Answer

RGB16 Sprites flickers Bug. 2018.1.x 0 Answers

Script to change SpriteRenderers sprite field 2 Answers

Help getting texture scale right 0 Answers

How change sprite animation texture?? 2 Answers


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