- Home /
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?
I know why... $$anonymous$$y user script is registering the first avatars it encounters and then it references only to them. So each time I enable a new room the triggers will not work for the current avatars but only for the initial avatars - which is a whole new problem.
Answer by Bodhid · Aug 18, 2017 at 02:23 PM
Call avatars = FindObjectsOfType(); just after a new room is enabled. That should do it
Answer by NeedsLoomis · Aug 18, 2017 at 04:41 PM
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.
Answer by Eco-Editor · Aug 20, 2017 at 02:44 PM
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.
I've solved it by this code:
avatar = FindObjectOfType<>();
if (avatar == null)
{
return; //if the script to reference is disabled than return
}
else { //other wise execute the function. }
I didn't even use the else option... I have two scripts ins$$anonymous$$d of making an array this worked.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How To Stop Movement With RigidBody.AddForce 2 Answers
Script controls 2 game objects not independently 1 Answer
Trigger Sound more than Once 0 Answers