unity load an player made C# script

Hi all

I am working on a game in which the player write his own C# script and apply them to an objects.
And in one scene the player write his code and game saves it as X.cs, if I run the game in the editor it saves the scripts in assets and if I run the game in a PC build it doesn’t work probably because the script isn’t included in the assests folder.
short story:
When I run the game in the editor the game works perfectly OK but on the build it doesn’t run the script.

here is the script that add the player script in to the object:

public bool once = false;
public string script;
public InputField loadScript;

// Use this for initialization

void Start() {

}

// Update is called once per frame
void Update () {
    script = loadScript.text;
    if (Input.GetKeyDown(KeyCode.LeftControl) == true) {
        if (once == false) {
            System.Type MyScriptType = System.Type.GetType(script + ",Assembly-CSharp");
            gameObject.AddComponent(MyScriptType);
            once = true;
        }
    }
}

}

is it even possible to run an external C# script like this if not how can I replace it?

I have read about lua scripting(moonsharp, lua interface, Nlua) but i don’t know how to work with it.

thanks for the help.

C# is compiled into IL. The IL is then converted JIT (just in time), into the native assembly language of the host machine.

You might be able to write a. NET runtime that is interpreted (instead of compiled) to IL instead.

I’m not entirely sure how’d you go about this. You might actually just have to write an interpreter “in-game”, and not use the C# compiler.

It would be easier to almost just let the player write in Python, or Lua, and interpret line by line, like Python’s Idle.

Any compiler questions, just let me know. It’s a hobby of mine.

Here’s a good link on .NET and C#:
https://msdn.microsoft.com/library/z1zx9t92
Best of luck :slight_smile:

I dont know if this is still relevent, but i know that in older versions of unity (i think it was 5.1) you could make a folder called Resources inside of the Assets folder. In your game you can call a function called Resources.Load(“script name”) and it would allow you to add scripts to objects in a build. that sounds like its exactly what youre looking for