Add Script from Project to GameObject

Is this possible?
I’m creating in runtime a script and saving it in the project but i cannot find the way to get that script and add it to a gameobject via script.
Steps:
Create GameObject via Script
Create Script via Script
*Get the Script created
*Add to the GameObject

Thanks!

How do you “create” a script? You cant instantiate MonoBehaviours.
Looks like I’ve been wrong, you can. But still, do it like this:

You add components like:

MyScript script = gameObject.AddComponent<MyScript>();

/* init script here */

The problem is that in that moment MyScript do not exist. Is just a file. Im creating it with the StreamWriter and adding the lines.
If there a way to seach scripts by the name “MyScript.cs” or something like that

You can’t load a script runtime, it has to be compiled into bytecode.
What are you trying to achieve, exactly?

You can load DLL’s at runtime though, is that an option? Building a .NET assembly.

I’m developing a ScreenManager where with an editor popup you can create the Screen template. So i create the GameObject, the Prefab and the Script, but i would like to add that script into the prefab so users will not have to drag and drop.

Oh, so it’s not at runtime, its an editor extension?

Well, you still need to build it somehow, after creating the MyScript.cs file, you need to wait for unity to build it and then you will be able to add it. But there’s no onBuild callback I know of…
You’ll have to instruct the user to add the script manually I think, but would love to know if there’s a way for this actually!

1 Like

There are some reflection tricks you could use. But the whole scheme doesn’t sit right with me. I guess I’m not understanding your use case properly.

I have an editor window where the steps i want to do are:
Create GameObject (done)
Rename it (done)
Create a script in editor runtime (done)
Create the prefab of the GameObject created (done)
Add the script created to the gameobject created (can’t)

I once tried this but ended in tears. You cannot add a script that is created through a script to an object. Unity needs to compile the script before it can be used and, this is not done through a script. Sorry, I tried for maybe 6 1/2 hours one day to no avail.

1 Like

Well, its not so bad to drag and drop something xD

Yeah. Good luck though!

I’m still not getting why an AddComponent + Reflection combination won’t work. You can use DidReloadScripts to get a call back after compilation is finished. Then you can scan the assembly for the script you just created with Reflection. Then you add the component with AddComponent.

It requires a fair bit of coding know how, I don’t have the time now to do a mock up for you. But if you are writing editor scripts then you should be able to handle it.

Run time compilation is possible on some platforms. Check out Emit. That said most games with run time compilation use an interpreted scripting language like LUA. Its far easier to implement and control.

Edit: Its also worth noting that by putting logic into data that can be parsed by your own system you can often avoid adding new scripts at runtime altogether.

Very interesting. I will have to look into Emit. I might be able to use it one day! :slight_smile:

Just be careful on the platforms, I know you can’t use it for iOS. Apple wants to be able to approve every single line of code that runs on their machines. I think the other platforms are mostly okay.

Thanks for your answer. Now i’m able to know when compiler fnished, but i don’t know how to search the file in the assembly. Im trying with this:

Assembly assembly = typeof(string).Assembly;
        Type type = assembly.GetType("ScreenBla");

        Debug.Log(type.Name);

With this, now i can search the class type!

[DidReloadScripts]
    static void ScriptsReloaded()
    {
        if(scriptName == "") return;

        foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
        {
            foreach (Type t in a.GetTypes())
            {
                if(t.IsClass && t.Name == scriptName)
                {
                    Debug.Log (t.Name);
                }
            }
        }
    }

My problem now is that scriptName is a static string that in a previous method has value, but when finished compiling returns to be null :confused:

I need in a way get the name of the script i had created.

Perhaps if i save it locally in a .txt and read it from there. Is that dirty?

Finally i could thanks to BoredMormon!

What i did was saving in a .txt the name of my class and doing what i posted up here.
Then scriptName matches with the type.Name and i add it to a GameObject that a created!