Calling constructor of scripts that are being instantiated

Hi,

I have a prefab that when I instantiate it, I like to call constructor of a class inside it so it’s fields values without making all of them public and assigning them by slow GetComponent.

Is there such feature available in Unity?

Thanks

It is not possible as it would seem in “usual” c# development.

If you do not have to pass any value, use the Start or Awake methods.

If you need to pass values then you call have an Init method. But you’d have to use some GetComponent at some point.

GetComponent is slow if you use it in Update but once at the starting of an object won’t affect.

I would recommend to consider a pool of objects, so that all needed items are created at the beginning of the scene and you just activate/deactivate when needed.

That also allows to set all values at the beginning and use GetComponent only once. Then only using the Init method when activating an object.