1

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?.

Fattie
  • 27,874
  • 70
  • 431
  • 719
Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186
  • 1
    If it's necessary to check the time each frame then you have to use Update. For better performance I would recommend not using GetComponent<>() in the actual update. Use it once on Start() to get a reference to Scale2 and the just use that reference. – Uri Popov Apr 04 '16 at 10:53
  • It's very likely you need to start using Unity's UnityEvent, which is extremely simple and is one of the best things in Unity: http://stackoverflow.com/a/36249404/294884 – Fattie Apr 04 '16 at 11:53
  • @JoeBlow Thanks for your advice but i have the same problem of pause. Additionally timer speed can be speed up by user using GUI. so invoke also not available for me. – Muhammad Faizan Khan Apr 07 '16 at 07:20
  • @JoeBlow "Never use "else if" for any reason". what does it mean? it somehow not possible i always use it in update as this question depicted – Muhammad Faizan Khan Apr 07 '16 at 07:20
  • @UriPopov GetComponent<>() not use in update? any reason please – Muhammad Faizan Khan Apr 07 '16 at 07:21
  • @MohammadFaizanKhan The reason is that the GetComponent operation is expensive. Update is called once every frame of the game. Generally you want to avoid using it because it might cause performance issues. The best approach is to call GetComponent<>() once in Start() or Awake() and then just use the reference you got. Like in Programmer's answer. Cheers :) – Uri Popov Apr 07 '16 at 07:37

1 Answers1

0

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.

FullStackForger
  • 1,060
  • 2
  • 12
  • 18
  • 1
    you simply use UnityEvent here, it has been available for 5+ years now. It is extremely easy, couldn't be simpler: http://stackoverflow.com/a/36249404/294884 – Fattie Apr 09 '17 at 17:00