- Home /
How can i create shortcut for creating new c# script and adding it to selected gameoject in editor?
I am trying to write a editor script that can create a new c# script and add it to the gameobject selected in the editor.I only have rough idea that i need to use reflection.
I'm curious - what's the use case?
Do you just mean a monobehaviour stub (like you would get if just creating a new script in the project view)?
It's not going to be easy - seems to me that at the very least you're going to have to write the new script to disk, and then allow the editor to load it and compile, before it's available to be added to any gameobject. This is some degree of faff to go through unless you're getting big gains from it. And it seems to me that since you'll always have to go and add some content to this monobehaviour class afterwards anyway, you're unlikely to gain much.
Plus, the idea that one might want to create a monobehaviour class for a specific gameobject feels odd to me.
Not saying it's completely crazy, but some idea of why you want to do it might help with suggesting solutions or workarounds that target your needs.
actually it just for curosity, i want to understand how unity creates new c# script and thought it would be good if i have a shortcut to create new script and add to a gameobject rather than first creating new script and then adding component to gameobject(it is slow shortcut would be fast). what problem i am facing is that unity also creates meta data with script for reference so i need to know unity way of creating new c# scripts
I don't think the creation of the script is complicated. It's just a text file. Writing editor code to do that is pretty simple. If it's just for curiosity then I$$anonymous$$O you're much better off focusing your efforts elsewhere. The difference in speed between creating a script and then attaching it to a gameobject, and using the shortcut you're talking about, is a difference that will make no difference.
Answer by PseudoLord · Jan 03, 2020 at 06:20 AM
You can try Ctr+Shift+A for windows when selecting a gameobject. It is used to add components, just type the script you want to create then and I guess it's the same use case then. The drawback is that you can't specify the folder. It creates the script in Assets folder directly I think.