FindObjectsofTypeAll()


I was just trying to destroy all the enemies present in the scene at special event for player get powerup-tag “clear enemies” and its working well but it is trying to destroy prefab also and unity saved me and throw an error
“Destroying assets is not permitted to avoid data loss”

its getting prefabs also I think how to avoid or check for that type of object is in present in editor or not ?
or reason is anything else than how to solve this

Hi,

Could it be that a Destroy method is trying to destroy a prefab?

aaah yeah. You’re telling Unity to go through the resources directory looking for the enemies and telling it to destroy them.

Try replacing your foreach line with:
foreach(Enemy go in FindObjectsOfType<Enemy>())

That’ll look for Enemies in the scene as opposed to enemies in your resource directory. Does that make sense?

with your suggestion got this warning
Warning|CS0618|‘Object.FindObjectsOfTypeAll(Type)’ is obsolete: ‘Please use Resources.FindObjectsOfTypeAll instead’

and run the game end got same error that before

yup you are so right but I want to avoid that obviously
how could I ?

Try the one I suggested: FindObjectsOfType not FindObjectsOfTypeAll. They’re different methods.

1 Like

In your code, I’m reading Resources.FindObjectsOfTypeAll. With this, you are accessing the resources, which include the prefabs. If you want to get all enemies in the scene, you call FindObjectsOfType<Enemy>(); (or whatever you named your enemy class).

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms