Hi guys,
I’m pretty new to csharp in unity. I’m creating a game (similar to harvest moon / stardew valley) where the player can put down growable seeds- they grow (change state) over time. What I’m finding is that when I plant them in game, the timers to change their state (currently only a sprite) fires on all objects at the same time. For instance - I plant 3 seeds 5 seconds apart, and they all bloom at the same time. If I place any more after the time has expired, all seeds start in the bloomed state.
I know this is wasteful as I’m dropping a lot of things with updating scripts. It’s just a test at the moment to see what happens and It would be great to understand what I’m doing wrong.
I’m creating my growable with the following code in my player class:
if (Input.GetKeyDown(KeyCode.Space)) {
Instantiate(placementObject, interactionObject.transform.position, Quaternion.identity);
}
My early code looks something like this:
using UnityEngine;
using System.Collections;
public class Growable_Flower : MonoBehaviour {
SpriteRenderer spriteRenderer;
public Sprite stageOne;
public Sprite stageTwo;
public Sprite stageThree;
float timePlanted;
float growTimeStageOne = 0.3f;
float growTimeStageTwo = 0.5f;
void Start () {
spriteRenderer = GetComponent<SpriteRenderer>();
}
void Update () {
if ((Time.time - timePlanted) * Time.deltaTime > growTimeStageOne) {
this.spriteRenderer.sprite = stageTwo;
}
else if ((Time.time - timePlanted) * Time.deltaTime > growTimeStageTwo) {
this.spriteRenderer.sprite = stageThree;
}
}
Any thoughts?
Thanks in advance.
JK