1
\$\begingroup\$

I have created 2 gameobjects along with its properties in the Unity Inspector. I have attached a screenshot below.

For prototyping, I like the Inspector very much, but for runtime I prefer doing everything by scripts. This way nothing can go wrong: I don't have to carry so many files around, and I find it easier to understand everything.

I would now like to change this so that these 2 components are created at runtime by script.

Is there perhaps a way to automatically create the script that would create these game objects?

I would appreciate it very much if I could save some time with such an automatic script creation.

Thank you for any input.

enter image description here

\$\endgroup\$
  • 2
    \$\begingroup\$ Generating the script automatically is possible, but it would be a pretty massive undertaking to do this in a fully general way. We can get a lot of benefit by using the built-in UnityEditor.SerializedObject to iterate over the inspectable fields of the object and its components. Where it gets hairy is assigning a value to each of these fields requires writing code to translate each possible value type into its corresponding literal/constructor, with some gotchas where the fields the editor sees aren't the same ones exposed to C# (eg. m_LinearDrag vs drag), and references are a nightmare. \$\endgroup\$ – DMGregory May 22 at 1:32
3
\$\begingroup\$

The short answer is no.

The longer answer is noooooooo. Well, it’s actually “almost, but...”

You seem to be mistrustful of Unity and want to do everything manually. Unity is designed to be the opposite of that. Sounds like Unity is not the engine for you in that case.

Some reasons why what you want to do is not a good idea:

  • technically impossible. On the loading thread, everything gets created and assigned “at once”, and then OnEnable/Awake/Start are called all “at once”, at the correct time. With a C# implementation, OnEnable and Awake gets called on each component as its added, before any fields are assigned values, and potentially before future components are added, meaning some things may be missing when a component expects them to exist.
  • slower. Things are being done on the main Unity thread rather than the loading thread, so everything must be done during update rather than in the background.
  • error prone. Accessing assets like fonts, textures, materials, etc. would require manually naming each asset as strings, and those assets would need to be stored in Resources or asset bundles.
  • slower again. If you put the assets into Resources folders, they must be loaded on launch, which slows launch time. If you put them in asset bundles, then you must build asset bundles separately whenever any assets change.
  • error prone again. If you ever rename a file, you would need to reconstruct the code that references it by name. (Note: using Addressable Assets May mitigate this, but I am not familiar enough with this system to know.)
  • larger application. All of your assets would need to go into resources folders. Unity can tell which assets you are using and remove the ones you aren’t during the build. However, no such luck with assets you control manually (i.e. everything, in your case). You may end up with a bunch of unused assets bloating the final app unless you regularly purge unused assets.
  • needlessly complex. You’re falling victim to the Not Invented Here problem. Because you don’t trust Unity’s loading system, you are re-inventing it yourself, in a necessarily more error-prone fashion.
\$\endgroup\$
1
\$\begingroup\$

It sounds like what you want to do is create a prefab, and load that dynamically from a script. I believe to execute a script you will need an object to which it can be attached. You can add a public field to your script which accepts a GameObject, attach that script to a game object, and drag-and-drop your prefab from the project explorer on to the GameObject's field in the inspector. Once that is done, you may reference that prefab from within the script, and use Instantiate(myPrefabField) to create a real GameObject from the prefab, as a template.

Check out this document by Unity

\$\endgroup\$
  • \$\begingroup\$ Thank you, but I have this as a prefab. However, I want to do this by code. I feel it error-prone and uncomfortable to move prefabs around between projects. I love script instead: Just attach your script, and it will do everything for you. \$\endgroup\$ – tmighty May 21 at 23:27
  • 1
    \$\begingroup\$ I get where you're coming from. But could it be you are experiencing discomfort with the nature or format of the code? Everything you see in the inspector is serialized either to binary or yml depending on your project preferences. That yml is the most concise way to represent the fields you are setting on the game object. The serializer takes that yml and turns it in to a game object. Essentially, there is code behind the scenes, and you can git diff it just like any other code, but it might just not feel right? \$\endgroup\$ – cwharris May 22 at 0:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.