Question by
Sabrekitty · Dec 02, 2016 at 03:42 PM ·
gameobjectarraysspriterenderer
[Unity2d Sprite Rendering Question] Grabbing and altering the sprite renderer of another game object
Hello all!
I'm new to coding and unity,
I dislike the unity UI system as it makes simple games massive. I've created buttons with colliders and raycasted them to get the positions.
The desired outcome: clicking on my buttons and changing the sprite of another object (walking up sprite/down sprite).
The problem: I can get one sprite to change in my array, but the moment I set up multiple sprites in my code, the sprite renderer stops swapping them out.
// This is my main game object I attach to the button manager
public SpriteRenderer spriteStack;
// The array
public Sprite[] headButtonSprites;
// Grabbing colliders to raycast
// This works on its own!
if (hit.collider.gameObject.tag == "up")
{
spriteStack.sprite = headButtonSprites[0];
PlayerBehaviour.dir = Vector2.up;
}
// The player still moves but the sprites stop swapping as soon as I add more to the array.
else if (hit.collider.gameObject.tag == "down")
{
spriteStack.sprite = headButtonSprites[1];
PlayerBehaviour.dir = -Vector2.up;
}
Comment