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
0
Question by davidflynn2 · Nov 20, 2012 at 12:27 AM · c#js to c#

problem sending information from js script to c# script

I am try to send the score and name from my Js script to my c# script.

This is my js script its called Quest1;

 var VoiceClip : AudioClip;
 static var Counter : int;
 var soundplayed = false;
 
 var script : Highscore;
 script = GetComponent("Highscore");
 
 
 function update ()
 {
     if(Counter >= 1)
     {
     script.name = "David";
     script.score = Counter;
     script.StartCoroutine("GetScore");
     
     print("There was an error posting the high score: ");
     }
 
 }
 
 
 function Start()
 {
 if (!soundplayed) {
     audio.PlayOneShot(VoiceClip);
     soundplayed = true;
   }
         
 }
 function OnGUI()
 {
     GUI.Label (Rect (10, 10, 1000, 20), "Metors Destroyed: " + Counter);
 }
 
 function Update () 
 {
 if(Counter == 6)
 {
 
 GetComponent(Quest2).enabled = true;
 GetComponent(Quest1).enabled = false;
 
 }
 }

This is the C# script I want to send the data to: its called Highscore

 using UnityEngine;
 using System.Collections;
 using System.Text;
 using System.Security;
 
 public class Highscore : MonoBehaviour 
 {
     public string secretKey = "12345";
     public string PostScoreUrl = "http://YouWebsite.com/.../postScore.php?";
     public string GetHighscoreUrl = "http://YouWebsite.com/.../getHighscore.php";
 
     private string name = "Name";
     private string score = "Score";
     private string WindowTitel = "";
     private string Score = "";
 
     public GUISkin Skin;
     public float windowWidth = 380;
     private float windowHeight = 300;
     public Rect windowRect;
 
     public int maxNameLength = 10;
     public int getLimitScore = 15;
     
     
     void Start () 
     {
         windowRect = new Rect (120, 40, 300, 300);
         StartCoroutine("GetScore");        
     }
 
     void Update () 
     {
         windowRect = new Rect (Screen.width / 2 -(windowWidth / 2), 40, windowWidth, Screen.height - 50);
         windowHeight = Screen.height - 50;
     }
     
     IEnumerator GetScore()
     {
         Score = "";
             
         WindowTitel = "Loading";
         
         WWWForm form = new WWWForm();
         form.AddField("limit",getLimitScore);
         
         WWW www = new WWW(GetHighscoreUrl,form);
         yield return www;
         
         if(www.text == "") 
         {
             print("There was an error getting the high score: " + www.error);
             WindowTitel = "There was an error getting the high score";
         }
         else 
         {
             WindowTitel = "Done";
                Score = www.text;
         }
     }
     
     IEnumerator PostScore(string name, int score)
     {
         string _name = name;
         int _score = score;
         
         string hash = Md5Sum(_name + _score + secretKey).ToLower();
         
         WWWForm form = new WWWForm();
         form.AddField("name",_name);
         form.AddField("score",_score);
         form.AddField("hash",hash);
         
         WWW www = new WWW(PostScoreUrl,form);
         WindowTitel = "Wait";
         yield return www;
         
         if(www.text == "done") 
         {
                StartCoroutine("GetScore");
         }
         else 
         {
             print("There was an error posting the high score: " + www.error);
             WindowTitel = "There was an error posting the high score";
         }
     }
     
     void OnGUI()
     {
     GUI.skin = Skin;
         
         windowRect = GUI.Window(0, windowRect, DoMyWindow, WindowTitel);
     
         name = GUI.TextField (new Rect (Screen.width / 2 - 160, 10, 100, 20), name, maxNameLength);
         score = GUI.TextField (new Rect (Screen.width / 2 - 50, 10, 100, 20), score, 25);
         
         if (GUI.Button(new Rect(Screen.width / 2 + 60, 10, 90, 20),"Post Score"))
         {
             StartCoroutine(PostScore(name, int.Parse(score)));
                name = "";
                score = "";
         }    
     }
     
     void DoMyWindow(int windowID) 
     {
       GUI.skin = Skin;
         
         GUI.Label (new Rect (windowWidth / 2 - windowWidth / 2, 30, windowWidth, windowHeight), Score);
         
         if (GUI.Button(new Rect(15,Screen.height - 90,70,30),"Refresh"))
         {
             StartCoroutine("GetScore");
         }         
     }
     
     public string Md5Sum(string input)
     {
         System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
         byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
         byte[] hash = md5.ComputeHash(inputBytes);
  
         StringBuilder sb = new StringBuilder();
         for (int i = 0; i < hash.Length; i++)
         {
             sb.Append(hash[i].ToString("X2"));
         }
         return sb.ToString();
     }
 }
Comment
Add comment · Show 1
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 Althaen · Nov 20, 2012 at 03:30 AM 0
Share

Why not convert your js to c# code?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sarmth · Nov 20, 2012 at 04:07 AM

I ran into this issue once myself. I found some information to suggest that it's due to C# being a pre-compiled programming language, whereas Javascript is read by an interpreter. Perhaps this is different in Unity3D but I have yet to find any information to suggest that C# and JS will actually work together and use the same Variables / Functions.

The only thing I was able to do was rewrite my code in C# and it all worked perfectly.

Comment
Add comment · 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

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

11 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

Related Questions

Multiple Cars not working 1 Answer

Convert JS to C# 1 Answer

Please Help Convert js to c# 1 Answer

Spawning portal 1 Answer

Making a Jetpack 3 Answers


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