Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by UberBruce · Nov 16, 2013 at 09:27 PM · spritereplacein

How do I replace a sprite in script?

I'm trying to replace a gameobject's sprite via script. I can't seem to find a way to do it. I've attempted replacing the texture after loading it via resources, but it doesn't help.

This is what I'm trying to do:

 Texture2D texture = Resources.Load<Texture2D>("Wall_Vertical");
 wall.renderer.material.SetTexture("Wall_Vertical", texture);


Since 4.3 released very recently there isn't much info out there. Anyone got an idea?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Skitzomaniak · Dec 11, 2013 at 06:45 PM

The easiest way I've found so far:

 Sprite spr = Resources.Load<Sprite>("SpriteName");
 SpriteRenderer sprRenderer= (SpriteRenderer)renderer;
 sprRenderer.sprite = spr;

No amount of fiddling around with the renderer's material got me anywhere, but getting the GameComponent's SpriteRenderer and just applying the sprite directly to it seems to work. I have no idea how this method is for performance, but it works.

Comment
Add comment · Show 1 · 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 Runalotski · Apr 03, 2014 at 11:53 PM 0
Share

Thank you this worked for me

I am trying to make a card game with playing cards and did not wnat 52 textures, this seems like i can mix numbers and a picture to represent suit.

And after a qick look i also found the next pice i wanted to be able to make the numbers red or black to make it easier to tell suit i used this pice of code

 sprRenderer.color = new Color (1, 0, 0, 1);




at the end of Skitzomaniak's code to make it red

i found the info here

http://answers.unity3d.com/questions/56924/changing-a-materials-color-in-c.html

avatar image
0

Answer by anisabboud · Mar 27, 2015 at 10:10 PM

Place awesome.png in Assets/Resources/ (you can have subfolders), and use:

 GetComponent<SpriteRenderer>().sprite = 
     Resources.Load<Sprite>("awesome");  // No file extension.

http://docs.unity3d.com/ScriptReference/Resources.html

There's also LoadAll that "Loads all assets in a folder or file at path in a Resources folder."

Comment
Add comment · Show 2 · 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 Whiteleaf · Feb 12, 2016 at 02:32 AM 0
Share

Sorry for reviving this, but you cannot have sub folders inside the resources folder. You can however put the resources folder inside a sub folder inside the assets folder.

avatar image allenallenallen Whiteleaf · Feb 12, 2016 at 08:39 AM 1
Share

Well, you should be sorry, because you're wrong. You CAN have subfolders in Resources.

It has been a feature since 7+ years ago.

http://answers.unity3d.com/questions/22180/loading-resources-from-folder-in-project-to-hierar.html

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

21 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

Related Questions

A node in a childnode? 1 Answer

Question Concerning transform.Rotate 1 Answer

Fade out sprite on click 4 Answers

Enemy Health Help 1 Answer

input key named : KeyCode.Escape is unkown 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