- Home /
I want to create an animation with multiple sprites images on a prefab ?
I have created a prefab "Block" of which there are three types and each of them have their own sprite. So I created the fowling script and added to my prefab object
public BlockType type;
public enum BlockType
{
RedBox,
YellowBox,
BlueBox,
}
public Sprite[] sprite;
public void SetType(BlockType t)
{
switch(t)
{
case BlockType.BlueBox:
this.GetComponent<Image>().sprite = sprite[0];
type = BlockType.BlueBox;
break;
case BlockType.RedBox:
this.GetComponent<Image>().sprite = sprite[1];
type = BlockType.RedBox;
break;
case BlockType.YellowBox:
this.GetComponent<Image>().sprite = sprite[2];
type = BlockType.YellowBox;
break;
}
}
And then I created another script which randomly selects a Box images and place it on the grid
for (int i = 0; i <= ySize; i++)
{
for(int j=0; j < xSize; j++)
{
GameObject block = Instantiate(Prefab);
grid[i, j] = block;
block.GetComponent<RectTransform>().anchoredPosition=new Vector2(x, y);
block.transform.SetParent(canvas.transform);
int random = Random.Range(0, 2);
switch (random)
{
case 0:
block.GetComponent<BlockControl>().SetType(BlockControl.BlockType.BlueBox);
break;
case 1:
block.GetComponent<BlockControl>().SetType(BlockControl.BlockType.RedBox);
break;
case 2:
block.GetComponent<BlockControl>().SetType(BlockControl.BlockType.YellowBox);
break;
}
x += xWidth*space;
}
y -= yHeight*space;
x = xStart;
}
}
The Problem i am facing is that the prefab shows only one sprite on the grid for each type, but i have a sprite sheet with multiple sprites of the block and i want these to come with animation on my grid using sprite sheet. How should i create an animation which will use sprite sheets to display animation of how blocks appear on the grid.
Your answer
![](https://koobas.hobune.stream/wayback/20220612081657im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Abrupt change of position between animation transition 2 Answers
Animated Overlay Armor Sprite 1 Answer
Animation based on replacing sprites in sprite renderer does not work? 0 Answers
Animation with Transparency Floats Above Ground 1 Answer
Is there a way to abstract sprite from animation clip? 0 Answers