Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by CrossBlade · May 16, 2013 at 01:53 PM · c#javascripttranslate

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
     }
 }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Graham-Dunnett ♦♦ · May 16, 2013 at 01:54 PM 0
Share

Why if you are still learning are you switching languages? What doesn't work? What have you tried?

avatar image CrossBlade · May 16, 2013 at 01:56 PM 0
Share

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...

avatar image CrossBlade · May 16, 2013 at 02:11 PM 0
Share

Actually these are all questions from me... But anyway, still thanks for your help!

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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); 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CrossBlade · May 16, 2013 at 02:11 PM 0
Share

Thanks buddy, I'll give it a shot now!

avatar image CrossBlade · May 16, 2013 at 02:36 PM 0
Share

AND it works! Thanks again~

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges