Problem Changing Sprites with an Array of Sprites
I am trying to change the sprite of a dog in my game when it moves left/right. The move method works (I followed a tutorial on that), and I wanted to temporarily change the sprite of the dog to it facing left/right, depending on what direction it is walking. I created an array of Sprite objects and have inserted the corresponding sprites I want to use via the inspector. However, the sprites are not changing. I know the syntax to change a sprite is at least correct because if I paste, for example, "this.gameObject.GetComponent().sprite = dogSprites[2];" in the Update() function, it will change it. But it will not change it from within the if/else if statements I have. And yes, I have checked that the code does go into the if/else if when the dog moves by printing to the console to make sure. Any and all help would be appreciated! Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DogController : MonoBehaviour{
private Vector3 destination;
public float speed;
public Transform[] waypoints; // points on in the screen the dog can move to
public Sprite[] dogSprites; // different sprites the dog can change to
public void Start()
{
this.gameObject.GetComponent<SpriteRenderer>().sprite = dogSprites[2];
}
public void Update()
{
if (Vector3.Distance(transform.position,destination) > 0.5f)
{
transform.position = Vector3.MoveTowards(transform.position, destination, speed * Time.deltaTime);
}
}
public void Move(Vector3 dest)
{
Sprite temp = this.gameObject.GetComponent<SpriteRenderer>().sprite;
if (transform.position.x <= 0 && dest.x > 0) // If the next waypoint is to the right
{
this.gameObject.GetComponent<SpriteRenderer>().sprite = dogSprites[0]; // change to move-right sprite
}
else if (transform.position.x >= 0 && dest.x < 0) // If the next waypoint is to the left
{
this.gameObject.GetComponent<SpriteRenderer>().sprite = dogSprites[1]; // change to move-left sprite
}
this.destination = dest;
this.gameObject.GetComponent<SpriteRenderer>().sprite = temp;
}
}
Answer by _DS_1 · Dec 03, 2020 at 01:10 AM
Move method is never called, are you calling it somewhere else?
The
Sprite temp
variable stores the actual sprite of the SpriteRenderer, and you change the sprite in the if/else but in line 34 you assign the sprite to temp. You should delete that line and 24, because you are overwritting the if/else changes.
Yes, $$anonymous$$ove() is being called in a different script (sorry, I should have mentioned that). And the reason I stored a the current sprite of the dog in temp is so that the sprite can change to the one of the dog facing left/right, and then change back to the original sprite once it is done moving. Is my logic in wanting to do that flawed? Here is the code that calls the $$anonymous$$ove() method:
private void $$anonymous$$oveDogToRandomWaypoint()
{
int randomWaypoint = Random.Range(0, waypoints.Length);
dogController.$$anonymous$$ove(waypoints[randomWaypoint].position);
}
And the $$anonymous$$oveDogToRandomWaypoint() method is called in the update method of that script. (There is no problem with the movement, it works just fine!)
Your answer
Follow this Question
Related Questions
Getting name of Spritesheet on runtime with C#? (Not individual SpriteName) 1 Answer
Get sprite size and position 0 Answers
Sprites being skipped over in sprite change script 1 Answer
Display pictures in assets folder in random order 0 Answers
Ordering my multi layered sprite characters (2D, Isometric) by the z Axis 0 Answers