- Home /
Trigger LateUpdate on inactive gameObjects
I've been using LateUpdate() to do some update logic on my game-objects. The problem is that it doesn't get called on in-active objects which I need it to do. How can I work around this problem? Should I let another gameObject's LateUpdate() go through and manually call a "MyLateUpdate()" method on all the relevant gameObjects? Would it be a problem in terms of performance?
Answer by Halfbiscuit · May 21, 2015 at 10:27 AM
Why are you deactivating the object? If it is because you want them to stop rendering then you can just disable the renderer and LateUpdate() will still be called.
What you suggest would work and would not impact performance. But it could be irritating to set up and if you add more objects that you want to be called then you would have to add references to them.
I would suggest finding a way that does not involve deactivating the objects, just the other components on them perhaps.
Thanks for the suggestion. Disabling the renderer is probably the simplest solution for me.
Turns out it doesn't work for me as I'm working with UI gameObjects that doesn't have a renderer. They have a CanvasRenderer but there is no way to disable it :S.
Answer by JCprogrammer · May 21, 2015 at 10:48 AM
I had the same issue though, my solution for it was something like this (Which might be like yours as well): Say you have specific type of objects that you want to update their properties even they are inactive. Call these type Foo, create a script then name it Foo. Then you will create a script called FooController. What this controller does, first it finds all the Foo typed objects in the field (the current scene), which can easily be done by FindObjectsOfType() function. Final step is to invoke the function you wanted to iterate as like LateUpdate() in a loop; Say we name this function FooLateUpdate(); in FooController's LateUpdate() function.
the implementation looks like this:
class Foo
{
public void FooLateUpdate()
{
//some code
}
}
class FooController
{
Foo[] fooObjects;
void Start()
{
fooObjects = FindObjectsOfType<Foo>();
}
void LateUpdate()
{
foreach(var item in fooObjects)
{
item.FooLateUpdate();
}
}
}