To find all objects of type ManagedUpdateBehavior simply use the Object.FindObjectsOfType method. eg:
private static List<ManagedUpdateBehavior> list; private void Start () { list = Object.FindObjectsOfType<ManagedUpdateBehavior>().ToList(); }
But truth he will not find inactive objects. And given that
Also objects are dynamically instantiated and removed from the scene.
there is no need to look for something each time and add components to it. It is enough to modify ManagedUpdateBehavior so that when it appears, it would list itself as a subscriber, let's call it UpdateSubscriber , and if you remove an object from the scene, it would remove itself from the list.
So, the modified ManagedUpdateBehavior , which you need to add to all objects, whose characteristics we will update (that is, call UpdateMe ) from the general manager will be like this:
public class ManagedUpdateBehavior : MonoBehaviour { UpdateSubscriber updateSUBSCR; // Добавляем себя в список void Start() { updateSUBSCR = GameObject.Find("UpdateSUBSCR").GetComponent<UpdateSubscriber>(); updateSUBSCR.addManagedUpdateBehavior(this); } // Удаляем себя из списка void OnDestroy() { updateSUBSCR.removeManagedUpdateBehavior(this); } public void UpdateMe() { // some logic Debug.Log("Update from: " + gameObject.name); } }
Next on the stage we create an object (our subscriber), with the name UpdateSUBSCR and attach a script to it:
public class UpdateSubscriber : MonoBehaviour { private List<ManagedUpdateBehavior> managedUpdateBehavior = new List<ManagedUpdateBehavior>(); public void addManagedUpdateBehavior(ManagedUpdateBehavior managedUB) { managedUpdateBehavior.Add(managedUB); } public void removeManagedUpdateBehavior(ManagedUpdateBehavior managedUB) { for (int i = 0; i < managedUpdateBehavior.Count; i++) { if (managedUpdateBehavior[i] == managedUB) { managedUpdateBehavior.RemoveAt(i); break; } } } public void updateAll() { for (int i = 0; i < managedUpdateBehavior.Count; i++) { managedUpdateBehavior[i].UpdateMe(); } } /* public List<ManagedUpdateBehavior> getManagedUpdateBehaviorinstance { get { return managedUpdateBehavior; } }*/ }
And also add the script below, which will separately call Update from the manager himself, which in turn will call all UpdateMe methods UpdateMe the objects in the list
public class Test : MonoBehaviour { UpdateSubscriber updateSUBSCR; void Start() { updateSUBSCR = GameObject.Find("UpdateSUBSCR").GetComponent<UpdateSubscriber>(); } void Update() { updateSUBSCR.updateAll(); } }
The answer is translated and slightly updated with https://stackoverflow.com/a/38541620/6104996