Can eval() declare functions (such as OnGUI())

Hi, my name is David,
And when i tried to make eval declare the OnGUI function like this:

function Start() {
eval("function OnGUI(){
GUI.Box(Rect(310,20,300,50),"Test 123".ToString());
}");
}

it did not return an error, but it didn’t work. Any tips? Thanks! -David

OnGUI needs to exist in the actual script so the engine can call it every frame.

var onGUICode : String;

function Start () {
	onGUICode = 'GUI.Box(Rect(310,20,300,50), "Test 123")';
}

function OnGUI () {
	eval (onGUICode);
}

By the way, your code wouldn’t actually compile the way you have it; a string must be on one line and quotes must be handled correctly. Also there’s no need to use ToString on a string.

–Eric

Yes, i know, i made a 10X more complicated script, i just wanted to simplify what it does. the thing is, I dont want to have a code like this:

var startcode:String;
var updatecode:String;
var awakecode:String;
var onguicode:String;
//etc...
function Start () {
//assign the codes to the variables
.....
eval(startcode);
}
function Update () { eval (updatecode); )
function Awake () { eval (awakecode); )
function OnGUI () { eval (onguicode); )
//etc...

And either way, technically, i wont make the code that is being evaluated, so i dont know what functions they will use. Or if they will create new functions. On another topic, I cant seem to be able to make a static variable! it says “unexpected static” or something of that matter. Please help me, this is very important. Thanks.

It looks like you’re trying to implement modding/third party scripting. If so - that’s definitely not the way to go about it.

Yes, i am, but I feel that LUA is underpowered, and for all and all, i REALLY like unityscript, it is easy to use, and it is very easy to adapt to it.

Eval has some limitations; you can’t just use a string as a substitute for a script. Actually I think it’s kind of impressive that it works as well as it does even with the limitations.

–Eric