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
0
Question by _Geo_ · Nov 14, 2013 at 02:51 PM · 2dspriteresources

Struggling to change the sprite on the SpriteRenderer through code?

Hey guys,

Trying to get used to the new Unity 2D stuff and I'm struggling with changing the sprites in the SpriteRenderer dynamically. Here's the code I'm using:

         Object loadedObject = Resources.Load(filePath);
         Sprite spriteObject = (Sprite)loadedObject;
         SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
         spriteRenderer.sprite = spriteObject;

So the loadedObject isn't null so it's definitely loading something but it's just loading the Texture2D (which is logical since the filePath is pointing at a .png). When I try to cast it as a sprite it fails and spriteObject is null. My question is how do I get a sprite out of this that I can set on a SpriteRenderer? Is it possible?

Some more info: The import settings on the png are set to importing as a sprite, if i drag and drop the .png in the scene it appears as a sprite. I can see the sprite in the Project view as an object under the .png, is it this I should be accessing? How do I load that?

Thanks in advance for any help!

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
0
Best Answer

Answer by _Geo_ · Nov 14, 2013 at 03:21 PM

Think I was being an idiot, just needed to use:

 Resources.Load<Sprite>(filePath);

and it loaded the sprite fine!

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
0

Answer by infinitypbr · Jan 06, 2014 at 09:17 PM

http://forum.unity3d.com/threads/211817-Changing-sprite-during-run-time

Eric5h5's answer, which is what I think you're actually looking for?

 var newSprite : Sprite;
 function Start () {
     GetComponent(SpriteRenderer).sprite = newSprite;
 }
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 Deepschalappatta · Jul 09, 2014 at 05:19 AM 0
Share

@Geo :How it possible on mouse over the image.

function On$$anonymous$$ouseEnter() { GetComponent(SpriteRenderer).sprite = newSprite; }

is this possible?

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

17 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

Related Questions

Dynamic setting of sprites cause many draw calls 0 Answers

Trail renderer not rendering over sprite background 4 Answers

Different Sprites for iPhone, iPhone Retina & iPad 0 Answers

Ray effect on sprite with obstacles 0 Answers

2D Sprite Animation - Rotate animation on axis 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