Add code from another script

Hey guys,

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.

Seriously don’t get it…could you explain it better or draw some exapmple…

Okay, here’s a quick thing that hopefully explains it more clearly

Why not do something like this (mind you, this is psuedo-code, just for demonstration):

In Main.js

function DoSmt(){
//Do something
}

In Receiver

var main:Main;

if(/*get some input*/){
main = AddComponent(Main);
main.DoSmt();
}

Conversely, why not just save the hassle and include the code you want to execute from the get-go? Sounds like a better idea for what you want to do :slight_smile:

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.

Thank you for all the comments so far.

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.

Sounds like you want code templates? Common snips of code that you just want to hit a button and insert.

Code Templates in MonoDevelop

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.

Hope this helps

Thank you all for the comments, I think I can continue forward now :slight_smile:

seems like you know how to tackle this, but take a look at runevision’s CreateScriptDialog which should give you a good headstart

Would making receive.cs inherit from main.cs not give you the functionality you require?