I wonder if it’s possible to insert code from one script to another.
Let’s say we have 2 scripts, one script main.cs and the other receive.cs.
The main.cs has a piece of code which receiver.cs doesn’t have. Now when i press a button, I want that specific piece of code from main.cs duplicated into the receive.cs. Is that even possible?
For all those who are planning to say, just put that code into receive.cs and activate with a boolean or whatever, that’s not the point.
I’m trying to figure out if I can duplicate a piece of code from one script to another, that would be great. Even greater if I can specify on which line it should be implemented.
I’d love to hear your thoughts, and if you think/know it’s possible.
It can be done, by reading the file as a normal text file, count lines from line you want until the end line you want, copy that line range and paste on a second file starting by the line you want. Not too complex… The important thing is why do you want to do this?
Can you say if there’s a particular problem or what is your intention.
Anyway reading file and copy text lines to another is pretty easy
There are ways to do what you want, but not quite how you are thinking about it. However, I don’t believe they really accomplish what your goal is. theRadRedd’s option is likely what you really want to do. You’ll have to explain what you really want if you want better answers.
I was thinking of creating menu items with standard code I use a lot throughout projects. So when I click on a menu item, a specific piece of code gets insert in the selected file. It’s just to increase my own workflow.
Depending on how you’re using the duplicated code, you may be able to use a non-duplicated method instead.
Re-used function, such as Vector3 GetCenter ( Vector3[ ] vectors ):
Create a utility class, make that a static method.
Similar behavior, like a button enlarging when hovered over, or turning grey when disabled:
Create a component that handles the shared behavior, add it to the object in addition to your specific component (either manually or by having the specific component add it on startup). Have your specific component grab a reference and call the shared functions as needed.
Also, you can edit the template file that new scripts are created from. Obviously, this won’t help with anything specific, but it can at least make the formatting more to your liking and add any universally shared parts.
I don’t know how compatible this is with the Unity version of MonoDevelop, but it should get you somewhere. I use surround with quite often when I’m coding, to add if blocks or try/catch blocks.