I didn’t found information about the update order of components on GO.
Let’s say I have 3 components on one GO. And let’s say Component B and C are consumers of the processing result from Component A … how do I assure that A is executed/updated before component B and C?
For example: component does some analysis like ray casting etc. Component B and C retrive the results of A and then do their thing.
There’s no information because it’s not reliable. In my experience the execution order seems to be based on the order the components were added, but because they’re not documented and aren’t meant to be used that way there is no guaranteed execution order.
If you want something to happen in a certain order you can take advantage of “Update” and “LateUpdate” (raycast in ComponentA’s Update, read that data in ComponentB’s LateUpdate), or you can directly call a ComponentB.ProcessData() from ComponentA.
Why not do that explicitly? Don’t do raycasting etc. in A’s Update(), instead have a function in A that does the update. And call it from B and/or C. You could even have something like this (i.e. cached-for-one-frame raycasting, pseudocode), but I’m not sure if that’s worth the complexity:
// A.js
private var doneThisFrame = false;
function DoRaycastingIfNotDoneYet()
{
if( doneThisFrame ) return;
// ... do raycasting
doneThisFrame = true;
}
function LateUpdate()
{
doneThisFrame = false;
}
// B.js
function Update();
{
var a : A = GetComponent(A);
a.DoRaycastingIfNotDoneYet();
// ...
}
Thank you both for your answers. Having to call an update function before acessing one of their properties, doesn’t seem very sexy to me.
I thought I could use components in this case a bit like a state machine were different GUI aspects/interactions are split up into components. And depending on the current GUI/interaction state, only a subset of the components are active. I easily could enhance it or reuse specific aspects in other projects. At least that’s my idea … I guess it takes some time to think the Unity’s way.
Yoggy suggested to use the MouseOver event, but I prefer having the logic on one GO and only havin data components on the others.
(edit:) is there something like OnEarlyUpdate() … something that gets called before Update (a bit like LateUpdate) ?
are OnMouseEnter called on each GO before any Update on any GO is called or is it that there is one major iteration over all GOs where a couple of triggered event functions are called?