- Home /
How to script independent time tracking of prefabs?
Hey friends,
i want to instantiate a prefab with a script attached to it. The script has a time dependent variable. Problem is: - The value starts changing right after starting the game, NOT with instantiation of the prefab. - Every prefab shares the same value, instead of being independend.
So i want to track the time of each prefab individually, with the instantiation as the beginning, NOT the game start.
Hope you can help me, thanks!
Please add the relevant code parts of your script. How you track time and how you instantiate the object.
Thanks for replying! I already fixed it.
time += Time.deltaTime;
I forgot the plus sign in this equation!
Answer by latishock · Jan 29 at 02:21 PM
@Hardfaile Easy fix for you will be something like this:
private float prefabStartingTime;
private float prefabCurrent;
private void Awake()
{
prefabStartingTime = Time.time;
}
private void Update()
{
prefabCurrent = Time.time - prefabStartingTime;
}
Of course, it will be better to implement prefabCurrent as a property, but it's for you to decide) Hope you understood the idea)