Objects in Hierarchy into Scriptable object

I want to get objects in my hierarchy into a scriptable object

public class LevelData : ScriptableObject
{

public List enemies;
}

I want to get enemies I’ve placed in my scene into that List. Is there a way to do that?

FindComponentsOfType<Transform>() will get you every Transform in the scene, from which you can get every GameObject.

If you only are interested in one part of the hierarchy and everything below it, use GetComponentsInChildren<Transform>(); from that parent GameObject instead.

Both approaches work serendipitously because Unity ensures a Transform exists on EVERY GameObject.

Remember if this is an editor script, call Undo.RecordObject() so it gets marked for saving.

1 Like

How do I use FindComponentsOfType? I’m getting an error “Does not exist in the current context”.

Always start by googling for the docs:

https://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html

1 Like