Instantiated Gameobject timers firing at the same time

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

You code for calculating the time passed is wrong.

To fix it, you need to add

timePlanted = Time.time; 

to the start function. The if conditions don’t need the * Time.deltaTime, and you need to get rid of the else statement, otherwise stage three can’t be reached

I recommend you to rewrite your timing code to individual timers in each instance of your object. It will be very simplier. In this paradigm you should only increment field representing elapsed time (from the creation of object) by Time.deltaTime and compare the new value with your desired time to “grow up”.

I can’t see you setting timePlantedanywhere. If you aren’t, technically all your plants are planted at the same time and will be ready at the same time.

Also i don’t think your math makes sense

if ((Time.time - timePlanted) * Time.deltaTime > growTimeStageOne)

That’s: time since the plant was planted, multiplied by how long it took to render the last frame. I.e. if you have a bad framerate, your plants will be grown-up more often and even then, if you get a fast framerate for a while, your plants might grow younger temporarily.

Just check

if (Time.time - timePlanted > growTimeStageOne)

and you can define the grow times like growTimeStageOne in seconds and not have magic numbers like 0.3f and 0.5f there

If you set timePlanted in Start() or Awake(), this should more or less work already.

    void Start() {
        timePlanted = Time.time;
    }