Is it possible to edit/write script code from another script

Hey guys, first time on the forums and in need of some help. I'll start with some context, I'm in uni and creating a game in unity, the game has a 'Spell-book' like script in which the behaviors of a 'spell' are defined, I need to create a tool so that I can have a panel for that script that allows you to edit current information about 'spells' such as cost of the spell and other variables, but i also need to have it so that the tool can add a new 'spell' and in doing so add new variables to the script. So if there a way in which i can edit/add to a normal C# scripts code with an editor script?

Sorry for the length of the post.

You can not declare new variables at runtime. What you CAN do, though, is add items to an ArrayList, or something like that.

I don't mean at runtime of the actual game but just whilst unity is running, or is that what you meant?

Also not sure if i should post a new thread for this, but can i treat a script like a text file and use the Windows class to write to a script and treat it like a .txt file? would it still work and compile in unity? Not at runtime through just as im using unity, if that makes any sense :)

Totally doable. I have built a couple of editor tools that exactly that. As mentioned, you can't do it runtime, but since you don't need to, its not a problem.

Yes, that's how you do it.

1 Like

Thanks for the help man :) can get on with work now :)

You can do it at runtime with the Mono.CSharp namespace (found in the Mono.CSharp.dll library).

There is even an asset on the store to assist with the process though I haven't tried it.!/content/23248


OK you need to build a spell system, the spell book will hold a list of spells, and those spells will have 'effects'.

So you just need to make a Spell class, with spell names, and probably best to have an Effects class that will trigger the effects of the spell, for instance a spell could shrink the player but double their hit points. So a Mighty Mouse spell would have a shrink effect and a mighty effect.

This looks like a comprehensive RPG tutorial ->

And if you go down to item 278 he goes over the spell system he uses.

Note just googled for the tutorial so not sure how good it is, also you might find RPG toolkits on the asset store with spell systems.

1 Like

Yeah I have a set-up like that, the 'spell-book' well it's called 'Spell Repository' is the effects class that you mentioned and I have a seperate script called 'Casting System' to handle the casting of spells and such but I'll take a look at the links provided, same with Ryiah, thanks for all the help guys :)

You'd be surprised how much flexibility you can achieve with regular delegates and variables.

Well sure... But writing code to code is just cool!

1 Like

Totally. But I don't think it meets the use case. Not unless you plan to allow moding on a heavy basis.

1 Like

Yea, you are probably correct. We do in a few cases, but they are all for pipeline and asset management, not the actual game.