• 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
3
Question by Krustbox · Jul 23, 2015 at 12:37 PM · animatorspriterenderer

Can't change sprite when Animator component is attached?

I'm new to unity. Why is it that when i disable the animator component, I can change a sprite just fine with code like this:

GetComponent(SpriteRenderer).sprite = newSpriteOne;

However, when i enable the animator component, even if there is no animation active, this code no longer works. It doesn't give any errors, just simply wont change the sprite.

I've noticed that when an animation is played, it will revert to the default sprite for the object when the animation is complete. Does this have anything to do with it? Is the animator somehow locking in the default sprite and only allowing the sprite to change via animation? If so, is there a way around this to allow or change default sprite when there is no animation active?

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 Yarbius · Aug 06, 2015 at 06:49 AM 0
Share

I am having the exact same problem, did you find an answer, all I find on Bingle is unanswered questions

4 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Yarbius · Aug 06, 2015 at 07:01 AM

I found that for some situations you can just disable the animator and the sprite changes fine. Then just enable the animator before triggering it.

It depends on your situation if this will work. I found it works great for a prefab that just has a death animation (Like a shot exploding before its GO is destroyed)

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 NeverHopeless · Aug 06, 2015 at 07:27 AM

It is possible that you have defined Sprite curve in your animation like given in the picture.

alt text

Once you removed this curve from animation you can change sprite with no issues, since then it is not going to override your sprite you specified in code at runtime.

Hope it helps!


animator-component.png (19.0 kB)
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 emersondesign · Jul 09, 2017 at 09:11 PM

Just wanted to add a +1 to NeverHopeless' comment. Man this issue has thrown me for some black hole detours ... but more often than not the issue is indeed that the sprite/image in question has been pulled into some Animation keyframe curve ... and that makes some if not all related C# code work inconsistently. Ugh so annoying!

Anyhow - thanks @NeverHopeless

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 retry_entry · Oct 18, 2017 at 10:30 AM

There is a better solution. Use this script - https://github.com/RetryEntry/UnityAnimatorChangeSprite Please give thumbs up, cause I have spent two days searching through useless solutions like one that suggected. If you have 15 gameobjects with 15 sprites in animation chain you will spend up to 225 loops every frame searching for sprites to swap to... With such approach you CAN'T create for instance charactes with different clothes animated by one animator, cause you will spend 12000 loops every second on useless things like searching. With this script you will not spend time... It uses shaders. You can change it as you'l like, Just star please. I have spent two days... :/

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Color change of game-object works not in-game 0 Answers

Animated sprite's bounds not in sync with its visuals 0 Answers

Syncing a sprite mask to an animated sprite renderer every frame 1 Answer

Animating player in Unity 0 Answers

Sprite falls outside the collider2D 0 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