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!
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.
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.
Your answer
Follow this Question
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