Reputation: 10561
I have a seperate script of time which I use to show time in my scene. It contains hour and minute and seconds variable.I want to do some specified work e.g., code execution on specified time and currently i am doing something like this. in Update. I am running a function which check continously check time variable in order to run an animation.
void Update()
{
checkTrainArriveTime();
}
void checkTrainArriveTime()
{
if (timeManager.GetComponent<Scale2>().hour == trainArriveTimeHour && timeManager.GetComponent<Scale2>().min == trainArriveTimeMin
&& isTrainArriveConditionExecute)
{
isTrainArriveConditionExecute = false;
PlayAnimationClip("Start");
}
else if (timeManager.GetComponent<Scale2>().min != trainArriveTimeMin)
{
isTrainArriveConditionExecute = true;
}
}
As Time will match this function will play the animation. Now I have 50 script attached to 50 different game Object. It is working fine but It definitely not the right way to use Update Event. In my code, It is necessary to check time on every frame and extra load on update. Is there any efficient way to do this Job?.
Upvotes: 1
Views: 177
Reputation: 1090
I can see your struggle. You are right, it is definitely not the best way forward.
The best option I can see here would be creating Animation Manager which is a singleton instance (there is only one instance allowed per application).
I would suggest moving your animation triggering logic to an Update
method of AnimationManager
.
Once you have done that. You will be able to access its instance calling AnimationManager.getInstance()
method.
Next step is creating internal registry that would be nothing else than just a list of your registered game objects that you want to trigger animation for.
I don't know what exactly is your timeManager
but I can imagine it is probably an instance of TimeManager
controller that you drag and drop onto your public timeManager
property. Consider turning it into singleton as well or at least moving assignment of timeManager.GetComponent<Scale2>()
into Awake()
method.
It is important to not to call GetComponent()
method from inside of Update()', as it has an impact on performance.
GetComponent` is quite expensive to call.
Hope it helps.
Upvotes: 0