- Home /
Time since object was instantiated?
I want to scale an object based on how old it is. I'm sure there's a simple way to do this, but I don't see it...?? Thanks for any assistance :)
Answer by ncallaway · Jan 05, 2014 at 07:23 AM
You can capture the current gametime in the object's Start(), and then compare it against the current gametime in Update():
private float initializationTime;
void Start () {
initializationTime = Time.timeSinceLevelLoad;
}
void Update () {
float timeSinceInitialization = Time.timeSinceLevelLoad - initializationTime;
}
If you want realtime elapsed instead of gametime replace Time.timeSinceLevelLoad Time.realtimeSinceStartup.
Happy to help! For non-answers, you can leave a comment on the specific answer or question you want to respond to. Look below an answer and there's a "add new comment" button. This, for example, is a comment.
It's generally better to use that feature to make responses that aren't answers (rather than the answer box).
Stick with it! It gets easier over time. :)
Answer by Seth-Bergman · Jan 05, 2014 at 07:28 AM
when you create your object, you can store its creation time.. create a script and add it to the object, then use:
var startTime : float;
function Awake(){
startTime = Time.time;
}
now that you have stored the creation time, you can see how long it has been around using:
Time.time - startTime
however, if all you want is for your object to get bigger over time, all this is overkill.. for example, to scale consistently over time, you could just say something like:
var scaleSpeed : float = 0.1;
function Update(){
transform.localScale += Vector3(scaleSpeed,scaleSpeed,scaleSpeed) * Time.deltaTime;
}
or to scale up periodically:
function Awake(){
InvokeRepeating("ScaleUp",10,10);
}
function ScaleUp(){
transform.localScale += Vector3(scaleSpeed,scaleSpeed,scaleSpeed);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613125603im_/https://answers.unity.com/themes/thub/images/avi.jpg)