Search Unity

Findobjectoftype on multiple frames?

Discussion in 'Editor & General Support' started by swantonb, Sep 17, 2021.

  1. swantonb

    swantonb

    Joined:
    Apr 10, 2018
    Posts:
    172
    Hey ive got a dumb question: can you findobjectsoftype somehow on multiple frames? I get big spikes in my void start when i try to findobjectoftype. I need it so not searching them like this isnt an option. Did someone else stumbled across this and fixed it somehow? Coroutines and waitforframe wont work but maybe with async there is a way? Anyone got experience with async?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    FindObjectsOfType is not a performant method in general.

    A better option may be to maintain your own list of objects that you're interested in and use that list directly. Simply add objects to the list as they are created and remove them as they are destroyed. Then there is no need for any Findxxx methods at all.
     
    swantonb likes this.