Locate Procedurally Generated Objects in Scene

Hello,

I am a novice to Unity and was having a bit of trouble locating randomly generated game objects. I am using the Object Placement Toolkit to generate a large amount of game objects which are only accessible once the program starts (i.e. they are not visible in the Hierarchy window until after I hit play).

What I want to know is if the player object and any one of these game objects overlap at any given time during the simulation. I have written a script which returns the location of game objects that I place manually within the scene but do not know how to do this with objects that are not available in the inspector before runtime.

I am not using a central pool manager for these objects but instead have individual pools for different areas where different types of objects will spawn. The amount of objects which spawn in each of these areas is different on every run so I also need a way to index them (which is likely a similar process to locating them).

How can I access the information of game objects which are not available to me when I am building the system? I have not been able to find a good explanation on returning the locations of procedurally generated objects. I am coding in C#.

Thanks,
-R

The lazy and bad way is gameobjects.find and the other functions like it. But i’m on a personal mission to destroy the routine use of these functions and will personally come slap you with a salmon if you use it regularly. (it’s very slow, not a complete crime to use it during level initialization but anything that runs during gameplay and it’s a bad idea to rely on it)

The better way would be to have your procedural objects register themselves with their local pool object when they spawn. Then you can just ask the local pool object for it’s list of local procedural objects.

But if you’re just wanting to check if you overlap one, can you not just use the collision system? colliders, physics.overlapsphere etc.

1 Like

When you make a procedurally generated object, I assume you mean your instantiating a Prefab? When you call instantiate it returns a reference to this object. You can make sure to keep this list somewhere as @Mordus pointed out like this:

List<GameObject> areaOneStuff = new List<GameObject>();

GameObject newObject = Instantiate(prefab,position,rotation);
areaOneStuff.Add(newObject);

That a rough idea of how you do it. Actual implementation would depend on how your project is setup. Also you mention using a ToolKit. If its the thing instantiating all these objects you need to find out if its keeping track of it in anyway. Maybe contact the developer. If it doesn’t , you’ll need to modify it so it does or write your own code to do this.

1 Like

I suppose I could… I wanted to stay away from using the Unity physics system as I am only building this for a visualization, but that could work just as well, thank you.

I looked through the code and didn’t see any Instantiate() calls, but I will look again. I wasnt exactly sure what I was looking for at the time, thank you