- Home /
Making a script prefab
Can I make a script prefab? I mean in the inspector set all of its proporties and in game code instantiate and attach it to object?
I only could do it in a prefab containing an empty game object, but it seems like unnecessary overhead and even then I cannot attach it to any object I want.
Answer by DaveA · Apr 08, 2011 at 12:06 AM
You can use AddComponent to add a script to any object at run time. Or you can create, as you say, an empty object, add script(s), turn that into a prefab. With AddComponent you don't get the Inspector setting defaults on your vars, with the prefab you do.
You have two options, but if you need to use the Inspector, the script has to be on some object for it to allow you to diddle with the default values. If you save that as a prefab, then you can use Resources.Load to instantiate it.