- Home /
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 ?
Check your folder name and path, that might be wrong.
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;
Thank you for posting the solution, it helped me sort out my problem :)
Hello, have you tried simply writing:
gameObject.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("img1");
?
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.
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;
}
can you suggest a way to do it without using public variables ?
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();
}
}
Same, I'm getting a blank too. Were you able to solve that?
I am not facing any such problems. Please check if your image path is correct.
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));
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.
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.
Yes your suggestions are better. Do you know how to get spriterenderer component without GetComponent?
Yes, just simply make an inspector property just like anything else.
public SpriteRenderer spriteRenderer;
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.
@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.
Sorry you are wrong about the sprite being packed into an Atlas, I do it all the time.
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.
Your answer
Follow this Question
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