What is the best way to update array in runtime?

Hi all,

I have a scene of rooms with animated avatars that respond to triggers when user is entering a collider. When the scene begins, the triggering script finds avatar in the scene and send them triggers. void Start(){ avatars = FindObjectsOfType(); }

But, when the room is disabled and a new room is enabled, the array of available avatars stays the same.

So I placed this line, in the Update function, and it worked, but I’m afraid it’s too costly for performance.
Maybe I can refresh the start function of the trigger script, from the script that changes the rooms?

Call avatars = FindObjectsOfType(); just after a new room is enabled. That should do it

You are probably looking for OnEnable(). When a script is disabled then enabled, this will trigger.

The other option is to just make a method containing the action, and call the method wherever your code activates rooms.

I have a multiplayer and I find that the method is called from two scripts instead of one. I’ve been suggested to FindObjectsOfType, but I don’t understand why unity is taking into account a disabled component? So I want to do something like this:
if (td.enabled) { td = FindObjectsOfType<>(); }

but it’s not working. I want the method to reference only the enabled component of this scritp that is on the local player.