- Home /
trying to set a javascript variable from c sharp
I am using C Sharp to connect to a Smartfox Server and pull data from a mysql database. When the data is returned (the users display name) I want to set this information to the variable declared in the javascript file.
public void OnExtensionResponse(object data, string type) {
if(type == "xml") {
SFSObject dataObject = (SFSObject)data;
switch ( dataObject.GetString("_cmd") ) {
case "logOK":
GameObject.Find("LoginCreds").GetComponent(UserInformation).user_display_name = dataObject.GetString("user_display_name");
break;
case "logKO":
loginErrorMessage = dataObject.GetString("err");
break;
}
}
}
Everything is working perfectly with the exception of the 'GameObject.Find... line. I get the following errors -
Assets/Game/Scripts/C Sharp/LoginGUI.cs(99,84): error CS0103: The name `UserInformation' does not exist in the current context
Assets/Game/Scripts/C Sharp/LoginGUI.cs(95,25): error CS0163: Control cannot fall through from one case label to another
Is it even possible to do this? Or should I just accept the fact that I'm going to have to walk away from Javascript and program the entire system in C Sharp?
Answer by Cyclops · May 27, 2010 at 08:21 PM
You can call one language from another - but they have to be compiled in the right order for access. See Accessing Javascript from C# for more details.
(Despite the name of the Question, it explains compilation order, and so applies to calling either language from the other.)
So grateful for this link!!!!! I'm using Javascript to access C# and I've got it working great. For those who come across this looking for the same answer, here's how I did it :
I moved all my C# scripts to a subfolder of "Standard Assets". I then created another root level folder called "Game" and put my javascripts in there.. "Standard Assets" folder gets loaded first, so this works.. you can even set the variables!! Thanks again Cyclops!
send me wt u have did, step by step what we want to declare and use in .js and c#.....i have done it ,but shows some error