2D Animation via C# Script

Hi, so, I have been working on animating a sprite. However I have run into a problem. It seems it will not change the the sprite to the sprites I want it to.

Code is found here: using UnityEngine;using System.Collections;public class PlayerParamedic : - Pastebin.com

Thanks!

Bump. Please! I can’t proceed until this is solved!

Looks like you’re assigning SpriteToRender, but at that point of update, it’s no longer a reference to the SpriteRenderer’s sprite. Try creating a SpriteRenderer member variable that references the SpriteRenderer component. Say, spriteRendererComponent. Then, in your update function, call spriteRendererComponent.sprite =

1 Like

Thanks so much for your response. However, I did do that. At the bottom you can see I am assigning rnd.sprite to SpriteToRender. That is what you mean don’t you? rnd is equal to GetComponent();

Or am I not understanding what you are saying lol.

Ah, I missed that one. Sorry. Hmm, Well, the only thing I can think of at the moment is that Resources.Load() loads a prefab that you still have to instantiate, but I don’t think that’s the problem here. I’ll dig up one of my 2D projects and see if there’s anything similar.

1 Like

I would appreciate that so much. I am stuck on my progress until I can get this working. I might have to just use the animator. However because my games animations will be so simple, I don’t want the extra hassle. Also, I am really bad with animations.

EDIT: while I can’t test this now, I wonder if I put as Sprite at the end of each Resources.load lines, maybe that would work? If you had any example of how you did this that would be great though!

I’m also suspicious of all of your state checks in your Update() function. You have checks for Input states as well as timer states. Comment out all of that and just get a basic timer going, that simply toggles between the 2 different sprites. If that works, then you know your problem is with your state managment, either your input states or timer states.

1 Like

Also, instead of

if(!SpriteToRender == rnd.sprite)

Try

if(SpriteToRender != rnd.sprite)
1 Like

Unfortunately, my project was large enough to have used the Animator component and controllers, so I don’t have anything to reference. Using “as Sprite” won’t be any different, since you are using the templated (generic) version of the function, so the function is already made specific for .

1 Like

I am curious as to how these statements are different? Just wondering. Thanks for all of your responses, I will test them as soon as I can and report back.

Since Sprite is an object, all your variables are references to that object.
In the statement

if(!sprite1 == sprite2) {}

Is similar to saying

bool notSprite1 = !sprite1;
if(notSprite1 == sprite2) {}

Essentially, you’re checking if sprite1 is null (0) or not null (!0).

Whereas

if (sprite1 != sprite2)

is testing if the objects that these two variables are referencing are actually the same object.
Neither of them has to be null. But in the first example, you are only testing if the value is null.

2 Likes

Thanks! I will test all of those things right away. I am going to assume it is the != that is causing it.

EDIT: changing the ! == to != fixed it.