- Home /
Having multiple gameobjects from one script, changing sprites in game
I'm making an farm game and I have three different kinds of plants that you can harvest. I have three gameobjects (the three plants) and they all have different sprites but use the same script. There's not much in the script, only a spriterenderer to change the sprite but I'm having trouble with that. To simulate a growing effect I want to change the sprite every time a day passes. However, I can't figure out how to do this. I now have a method that references to my 'Plant' class that gets implemented when the 'next day' button is clicked. This is in my main class:
public void ChangeCarrot()
{
carrot.SpriteChanger (CS1);
}
public void nextDay()
{
if (amountTurns == 0) {
amountTurns++;
}
amountDay++;
SetDays (amountDay, amountTurns );
ChangeCarrot();
}
This is in my plant class:
public class Plant : MonoBehaviour {
public SpriteRenderer spriteRenderer;
void Start ()
{
spriteRenderer = GetComponent<SpriteRenderer>();
}
public void SpriteChanger(Sprite sprit)
{
SpriteRenderer spriteRenderer;
spriteRenderer = GetComponent<SpriteRenderer>();
spriteRenderer.sprite = sprit;
}
}
So: I want to change the sprite of the separate gameobjects (you can add multiple kinds of the same plant) so that every time you would press the button 'next day' (which implements the method nextday()) to simulate a growing effect. I hope I explained it clearly enough because I've been working on this all day and I can't think anymore haha.