1

Is it a bad practice calling Action in the Update in Unity? Should I avoid it and use methods instead? I am trying to avoid using close ties between to classes.

public Action OnRotation;

void Update()
{
    OnRotation?.Invoke();
}

Or

private SomeClass someClass => FindObjectOfType<SomeClass>();

private void Update()
{
    someClass.Rotation();
}
Roman
  • 33
  • 7
  • Update is called every frame, do you need to call your func every frame? If not you would probably want to throttle your Action call. – Charles Dec 30 '21 at 00:13
  • yes , event use a little bit performance , but not a lot , So why you need event ? if you need it , just use it, I think it's fine. – TimChang Dec 30 '21 at 01:21
  • does [this](https://stackoverflow.com/questions/20645033/how-much-performance-overhead-is-there-in-using-events) answer your question? – rustyBucketBay Dec 30 '21 at 07:20
  • The performance hit is minor in your case. If you were to create an Update manager where all of your MonoBehaviour register to a single update, then you would start to actually see increased performances since Unity Update has a larger hit than a single update method. – fafase Dec 30 '21 at 08:32
  • Events, Delegates and Actions are garbage makers AND the messaging is slow. It's better to use Abstract methods in objects, as this is faster than Interfaces and Virtual functions. It's a little more work, and demands a bit more rigour to work with Abstracts, but the long term structural and performance benefits will pay off well before you get to the end of your project, especially if you stick to this "state" type pattern throughout. – Confused Dec 30 '21 at 14:39

0 Answers0