Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by adenovirux · Sep 16, 2015 at 03:32 PM · c#javascript

Js to C# conversion without error but can't work

Hi, everyone!

I had converted the following script from Js to C# in order to access the variable in another C# script. The C# codes can be compiled without errors but it just doesn't work, while the original Js one works well. Can anybody help me to check what going on? the original Js script:

 private var UDPHost : String = "127.0.0.1";
 private var listenerPort : int = 8000;
 private var broadcastPort : int = 57131;
 private var oscHandler : Osc;
 
 private var eventName : String = "";
 private var eventData : String = "";
 private var counter : int = 0;
 public var output_txt : GUIText;
 
 
 public function Start ()
 {    
     var udp : UDPPacketIO = GetComponent("UDPPacketIO");
     udp.init(UDPHost, broadcastPort, listenerPort);
     oscHandler = GetComponent("Osc");
     oscHandler.init(udp);
             
     oscHandler.SetAddressHandler("/eventTest", updateText);
     oscHandler.SetAddressHandler("/counterTest", counterTest);
     
 }
 Debug.Log("Running");
 
 function Update () {
     output_txt.text = "Event: " + eventName + " Event data: " + eventData;
     
     var cube = GameObject.Find("Cube");
     var boxWidth:int = counter;
     cube.transform.localScale = Vector3(boxWidth,5,5);    
 }    
 
 public function updateText(oscMessage : OscMessage) : void
 {    
     print ("get text message!");
     eventName = Osc.OscMessageToString(oscMessage);
     eventData = oscMessage.Values[0];
 } 
 
 public function counterTest(oscMessage : OscMessage) : void
 {    
     print ("get counter message!");
     Osc.OscMessageToString(oscMessage);
     counter = oscMessage.Values[0];
 } 
 

and the converted C#

 using UnityEngine;
 using System.Collections;
 using System.Threading;
 using System.Text;
 using System.IO;
 using System; 
 
 public class OSCListener : MonoBehaviour {
 
     
     private string UDPHost = "127.0.0.1";
     private int listenerPort = 8000;
     private int broadcastPort = 57131;
     
     private Osc oscHandler;
     
     private string eventName = "";
     private string eventData = "";
     private int counter = 10;
     public GUIText output_txt; 
 
 
     // Use this for initialization
     void Start () {
     
         UDPPacketIO udp = GetComponent<UDPPacketIO>();
         udp.init(UDPHost, broadcastPort, listenerPort);
         oscHandler = GetComponent<Osc>();
         if (oscHandler != null) {
             oscHandler.init (udp);
             print("build oscHandler");
         } else {
             print ("not able to build oscHandler");
         }
 
         
         oscHandler.SetAddressHandler("/eventTest", updateText);
         oscHandler.SetAddressHandler("/counterTest", counterTest);
 
     }
 
     //Debug.Log("Running");
 
     // Update is called once per frame
     void Update () {
     
         output_txt.text = "Event: " + eventName + " Event data: " + eventData;
         
         GameObject cube = GameObject.Find("Cube");
         int boxWidth = counter;
         cube.transform.localScale = new Vector3(boxWidth,5,5);    
 
 
     }
 
 
     void updateText(OscMessage Message)
     {    
         print ("get text message!");
         eventName = Osc.OscMessageToString(Message);
         eventData = (string)Message.Values[0];
     } 
     
     void counterTest(OscMessage Message)
     {    
         print ("get counter message!");
         string eventCounter = Osc.OscMessageToString(Message);
         counter = (int)Message.Values[0];
     } 
 
 
 }
 

Thanks a lot in advance!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Suddoha · Sep 17, 2015 at 10:45 AM

It's hard to tell whether this script causes the issue or any other one that might be used in it.

Why don't you simply use the original? You can put the JS file into the Standard Assets or Plugins folder so that it compiles first. You can then use the script in any C# script and of course, access the variables and methods, too. You said it can be compiled without errors, but are there any errors at runtime?

And you shouldn't use GameObject.Find in Update, at least not every frame.

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
avatar image
0

Answer by adenovirux · Sep 20, 2015 at 12:36 AM

Hi, Suddoha:

Thanks you for replying and sorry for the late reply.
I actually did try to put js file in Plugin folder and C# in scripts folder or make all of them in the plugin folder etc. But whenever I put the js file in the pulgin folders, the console shows " the name 'OSC' does not denote a valid type ("not found")" since this OSC receiver/Listerner file require a OSC class to be pre-compiled. There is also no errors at the run time.

Thanks for the reminder of the position of Gameobject.Find.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

implementing android activity? 0 Answers

Game got slow down On iphone 0 Answers

Help in conversion of variable types c# 1 Answer

Is C# or javascript easier to understand? 2 Answers

convert java 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