• 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 JasonB · Apr 27, 2014 at 08:10 PM · errorspritespriterenderer

Experiencing strange issue changing a SpriteRenderer's sprite at runtime.

I'm getting error messages when trying to change a SpriteRenderer's sprite at runtime.

The error doesn't seem to make any sense.

Here's the line that is producing the error:

 myRenderer.sprite = Skirt_UDLR;

myRenderer is a variable of type SpriteRenderer that is properly assigned in Start().

 myRenderer = GetComponent<SpriteRenderer>();

Skirt_UDLR is a public variable of type Sprite in my code that has been properly assigned with a Sprite in the inspector.

So, in theory, there's no reason the code shouldn't work, yet I'm getting this error in debug:

 NullReferenceException
 UnityEngine.SpriteRenderer.set_sprite (UnityEngine.Sprite value) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/SpritesBindings.cs:176)

But this error has nothing to do with my code, I don't think, as it's an error with a base Unity script. Anyone know what's going on here?

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

1 Reply

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

Answer by JasonB · Apr 27, 2014 at 08:44 PM

Well, nevermind. It seems the answer is that this can't be done, and the only way to change a Sprite at runtime is by creating needless animations. In this case, I'll need to create 45 1-frame animations and use an Animator to switch between them.

Very silly and wasteful.

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 Romano · Apr 28, 2014 at 12:15 AM 1
Share

Howdy. I've had success changing sprites at runtime using spriteRenderer.sprite. Are you sure its not something weird to do with your sprite? $$anonymous$$aybe if you tried changing the sprite to another one you could see if there's just something it doesnt like about that particular sprite?

I've got my sprites in an array, and I attach them in the inspector. Then I've got this:

 function Start()
 {
 
 customCursorSpriteRenderer = customCursor.GetComponent(SpriteRenderer);
 
 }
 

And in a function later:

 customCursorSpriteRenderer.sprite = cursorSprites[0];    

Doesn't seem any different from what youre doing apart from being in javascript and the array. $$anonymous$$aybe there is a bug?

avatar image OmegaVesko · May 11, 2014 at 02:52 PM 0
Share

Also confir$$anonymous$$g that doing spriteRenderer.sprite = someSprite has never caused me problems. In fact, doing this every frame (with an array of sprites, obviously) is how I usually handle sprite animations in Unity. Are you sure it's not something about your source asset that's causing problems?

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

22 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

Related Questions

I get an error if i try to use flipX : 'SpriteRenderer' does not contain a definition for 'flipX' 1 Answer

Change a prefab sprite using Random.range instantiate in a script 3 Answers

Force a SpriteRenderer to render at a position different from the actual transform's position 0 Answers

Stretching certain part of a sprite (like ui) 1 Answer

How to get Sprite color palette 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