Cycling through array not working. Pulling my hair over this one.
I have a simple script that, on a mouse click, should change the player sprite to a random one from an array of sprites - which it does. However, if the player keeps pressing the mouse button, it swaps back and forth from player sprite to random sprite, rather than cycling through the array.
What on earth am I missing?
public Sprite[] shapeshiftItems = new Sprite[]{};
public Sprite playerSprite;
private Sprite newSprite;
public SpriteRenderer rend;
void Start()
{
rend = GetComponent<SpriteRenderer>();
rend.sprite = playerSprite;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Shapeshift();
}
if (Input.GetMouseButtonDown(1))
{
UnShapeshift();
}
}
void Shapeshift()
{
int spriteRandomizer = Random.Range(0, (shapeshiftItems.Length));
newSprite = shapeshiftItems[spriteRandomizer];
rend.sprite = newSprite;
}
void UnShapeshift()
{
rend.sprite = playerSprite;
}
}
Comment
Your answer
Follow this Question
Related Questions
Gray Pixels put on sprites that weren't there before. 0 Answers
Sprite AI rapidly flipping to try and face the player. This is driving me insane. 0 Answers
Sprite image on UI appears much darker than actual image file. Please help! 1 Answer
Sprite rendering order issue 0 Answers
Trying to get gameobject from one list to follow gameobjects in another list 1 Answer