- Home /
About "translating" js into C#
Hello everyone, I was trying to "translate" a JS into a C# script. But I've just learned unity for 1 month and I kept making mistakes. I've finished tranlating most of it but there are still mistakes I wasn't able to correct, can anybody help me plz? REALLY need help here...with a desperate and thankful heart~!
Original one:
#pragma strict
public var objname = ""; //this is the field where you put the name to request
var formText = ""; //this field is where the messages sent by PHP script will be in
var URL = "XXX"; //the distant script URL
public var textrect = Rect (10, 90, 500, 50); //just make a GUI object rectangle
function OnGUI() {
GUI.Label( Rect (10, 10, 80, 20), "Object name" ); //text with object name
var c = gameObject.GetComponent("OnScreenKeyboardExample");
//objname = OnScreenKeyboard.editableString;
//objname.GetType().GetField(editableString).GetValue(objname);
//objname =
//GUI.TextField ( Rect (90, 10, 100, 20), objname ); //here you will insert the new value to variable objname
if ( GUI.Button ( Rect (10, 50, 100, 20) , "Try" ) ) //just a button
{
Login();
}
GUI.TextArea( textrect, formText );
}
function Login() {
var form = new WWWForm(); //here you create a new form connection
form.AddField( "objectname", objname );
var w = WWW(URL, form); //here we create a var called 'w' and we sync with our URL and the form
yield w; //we wait for the form to check the PHP file, so our script dont just hang
if (w.error != null) {
print(w.error); //if there is an error, tells us
} else {
print("Test OK");
formText = w.data; //here we return the data our PHP told us
w.Dispose(); //clear our form in game
}
objname = ""; //just clean our variables
}
Modified one:
using UnityEngine;
using System.Collections;
public class asker : MonoBehaviour
{
public string objname = ""; //this is the field where you put the name to request
public string formText = ""; //this field is where the messages sent by PHP script will be in
public string url = "XXX"; //the distant script URL
public Rect textrect = new Rect(10, 90, 500, 50); //just make a GUI object rectangle
void OnGUI() {
GUI.Label(new Rect (10, 10, 80, 20), "Object name" ); //text with object name
// string c = gameObject.GetComponent("OnScreenKeyboardExample");
objname = OnScreenKeyboardExample.editableString;
//objname.GetType().GetField(editableString).GetValue(objname);
//objname =
//GUI.TextField ( Rect (90, 10, 100, 20), objname ); //here you will insert the new value to variable objname
if ( GUI.Button (new Rect (10, 50, 100, 20) , "Try" ) ) //just a button
{
Login();
}
GUI.TextArea( textrect, formText );
}
IEnumerator Login() {
WWWForm form = new WWWForm(); //here you create a new form connection
form.AddField( "objectname", objname );
static function w = new WWW(URL, form); //here we create a var called 'w' and we sync with our URL and the form
yield return w; //we wait for the form to check the PHP file, so our script dont just hang
if (w.error != null) {
print(w.error); //if there is an error, tells us
} else {
print("Test OK");
formText = w.data; //here we return the data our PHP told us
w.Dispose(); //clear our form in game
}
objname = ""; //just clean our variables
}
}
Why if you are still learning are you switching languages? What doesn't work? What have you tried?
Actually I want to invoke a variable in C# to JS, but although I've tried a lot of ways, they don't work. So this may be the last way to do it...
Actually these are all questions from me... But anyway, still thanks for your help!
Answer by robertbu · May 16, 2013 at 02:06 PM
I spotted a couple of things:
Line 26:
StartCoroutine(Login());
Line 34:
WWW w = new WWW(url, form);
Your answer
Follow this Question
Related Questions
How to make this line work in C#? 1 Answer
C# to java translation 2 Answers
Translate Unity-scripts to uScript 1 Answer
java to C# conversion 1 Answer
Converting Variable to C# 1 Answer