C
C
ColdSpirit2018-10-23 10:20:58
C++ / C#
ColdSpirit, 2018-10-23 10:20:58

How to avoid problems with Execution Order?

Good afternoon. Whenever I create more than a few related components on a unit, there are problems that one component tries to work with another component that has not yet been initialized. This comes to the point of absurdity - it happens that everything works fine, and after a restart, bugs appear, because the unit has changed the order of creating components, and now everything needs to be double-checked.
I myself know 2 methods for solving this problem:
1. Use Script Execution Order Settings . I consider this a terrible crutch for those who do not know how to plan architecture. For adding, exaggerating, each created component there and adjusting its order is not the point.
2. Write your own component manager that will call Init() of the component in a certain order, described in the component itself, as an alternative to Start and Awake. This method is not far from the previous one, except that it is more flexible (each component can be written its own start time), and more convenient to use. Also a crutch.
Interested in what methods for solving this and similar problems exist not only for the unit, but in general in programming?
How do you solve this problem, what rules and techniques do you follow?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Pavlov, 2018-10-23
@ColdSpirit

In Awake, I use the initialization of the component itself, without using connections with other components. And in Start, all components are already initialized, they can be used.
Component A in Start receives a reference to component B. It is not a fact that B has already called Start and does not have the necessary references. There are two options
- B must also have a reference to A. Then A can transfer itself to B (before calling B.Start).
- B must have a reference to component C. There may be problems here.
In order for such links to be independent of the order, each component in Awake must place itself (register) in some container, through which you can get the necessary link in Start in any component.
The container can be
- static field of the class (if only one component is planned, then it turns out to be of the semi-singleton type - the field is set to Awake, and then used from everywhere during Start and after)
- a special list in the parent (if the component is not a singleton)
- IoC container

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question