The correct scripting order: from an empty scene to a populated scene.

Hi all,
I am a brand new Unity user. I’ve had many years of industrial software engineering experience, so I hope once I get to grips with the API it won’t be long before I can contribute to the community.

I’m not after C# syntax per se, more the order of Unity API execution for a scenario I have in mind.

I am interested in instantiating approximately 100 GameObjects/Prefabs, with a low rendering cost but each ‘unit’ has complete autonomy over its own actions. The ‘units’ are to be instantiated in random positions (not very difficult) on loading the empty scene. I already have a GameObject of a single ‘unit’ that I made in the editor, although, so it is not running on its own on rendering the scene I won’t have activated it.

I’ve come to understand a GameObject’s Update, Start and FixedUpdate, however, I want an empty Scene to instantiate these ‘units’ not an existing GameObject, which upon instantiation will begin their regular Start—>FixedUpdate(cycle) behaviour. Perhaps the best way to think of this is if you are feeling very lazy and you want to randomly generate 100 instances of a GameObject as the Scene loads without Duplicating or positioning them by hand in the editor, what is the best way?

I would appreciate if someone could at least point me to the specifics in the literature for backend Scene method execution.
Many thanks, Anthony.

Thanks

You need at least in GameObject in existence to act as an entry point. You only need to attatch your initialisation component to it.

Unity won’t run any code without at least one GameObject.

1 Like

Adding to BoredMormon’s statement, realize that a GameObject doesn’t actually have to be a thing that is visibly or physically active in the scene. Due to Unity’s design decision that every GameObject must have a Transform component, it might feel like that at first. But it’s entirely fine to have a GameObject at (0, 0, 0), have no mesh, sprite, rigidbody, collider, or any other “concrete” component on it, and just attach one or more of your own scripts to do whatever needs to be done, ignoring the automatically attached Transform component completely. I do that a lot for “manager” and “initializer” style scripts; works just fine.

Alternatively, you might look into attaching the [RuntimeInitializeOnLoadMethodAttribute] attribute to a static function. I’ve used that for a few simple in-editor purposes, but I don’t have any experience with how well it would work for initializing a scene at runtime.

2 Likes

Another option if you don’t need runtime randomness is to write a simple editor script to place all the GameObjects in the scene. That’s how I built and configured the 200 odd virtually identical physics objects for Pond Wars.

2 Likes

Thanks for this information, that was a very interesting and new way, for me, to look at things. Thanks.

An editor script sounds exactly like what I am after. Thanks for your help.