- Home /
Unity Crashes when C# script with code inside class but outside function is added to a GameObject
I copied a C# script from a coding example (a visual studio project) and whenever I add this script to a gameobject, Unity just closes. If I have the script on an object already, and then I add the code to the script and save, a nice red X error window pops up then Unity closes. Sometimes it only closes when I actually try to run the game.
Following is the script in its entirety. If I remove all the functions, it still fails, so the problem area is probably before the first function (before 'void OnGUI').
using UnityEngine; using System; using System.Collections.Generic; using System.Collections; using System.ComponentModel; /*using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;*/ using System.Net;
//using NatNetML;
public class DLLTestScript : MonoBehaviour {
// [NatNet] Our NatNet object
//private NatNetML.NatNetClientML m_NatNet = new NatNetML.NatNetClientML();
private NatNetML.NatNetClientML m_NatNet;
// [NatNet] Our NatNet Frame of Data object
private NatNetML.FrameOfMocapData m_FrameOfData = new NatNetML.FrameOfMocapData();
Hashtable htMarkers = new Hashtable();
Hashtable htRigidBodies = new Hashtable();
const int GraphFrames = 500;
int m_iLastFrameNumber = 0;
float m_fLastFrameTimestamp = 0.0f;
float m_ServerToMillimeters = 1.0f;
private delegate void OutputMessageCallback(string strMessage);
private Queue<NatNetML.FrameOfMocapData> m_FrameQueue = new Queue<NatNetML.FrameOfMocapData>();
void OnGUI() {
GUI.Label(new Rect(10, 10, 100, 20), "Hello World!"); }
// Use this for initialization
void Start () {
//Code from WinFormsSample
// Get the first detected ip address of this machine and use it as a default
String strMachineName = Dns.GetHostName();
IPHostEntry ipHost = Dns.GetHostByName(strMachineName);
/*int count = 0; ##Form Version##
foreach(IPAddress ip in ipHost.AddressList)
{
string strIP = ip.ToString();
if(count==0)
{
textBoxLocal.Text = strIP;
textBoxServer.Text = strIP;
}
count++;
}*/
// create NatNet server
int iConnectionType = 0;
//if (RadioUnicast.Checked) ##Form Version##
// iConnectionType = 1;
int iResult = CreateClient(iConnectionType);
// create data chart
//chart1.Series[0].Points.Clear();
//chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
Connect();
}
private int CreateClient(int iConnectionType)//From WinFormsSample
{
if(m_NatNet != null)
{
m_NatNet.Uninitialize();
}
m_NatNet = new NatNetML.NatNetClientML(iConnectionType);
// [NatNet] set a "Frame Ready" callback function (event handler) handler that will be
// called by NatNet when NatNet receives a frame of data frm the server application
m_NatNet.OnFrameReady += new NatNetML.FrameReadyEventHandler(m_NatNet_OnFrameReady);
// [NatNet] print version info
int[] ver = new int[4];
ver = m_NatNet.NatNetVersion();
//String strVersion = String.Format("NatNet Version : {0}.{1}.{2}.{3}", ver[0], ver[1],ver[2],ver[3]);
//OutputMessage(strVersion);
return 0;
}
private void Connect()
{
// [NatNet] connect to a NatNet server
int returnCode = 0;
//SERVER AND CLIENT IP'S GO IN HERE
returnCode = m_NatNet.Initialize("192.168.1.102", "192.168.1.102");
/*if (returnCode == 0) //##F
orm Version
//OutputMessage("Initialization Succeeded.");
else
{
//OutputMessage("Error Initializing.");
checkBoxConnect.Checked = false;
}*/
// [NatNet] validate the connection
NatNetML.ServerDescription desc = new NatNetML.ServerDescription();
returnCode = m_NatNet.GetServerDescription(desc);
if (returnCode == 0)
{
//OutputMessage("Connection Succeeded.");
//OutputMessage(" Server App Name: " + desc.HostApp);
//OutputMessage(String.Format(" Server App Version: {0}.{1}.{2}.{3}", desc.HostAppVersion[0], desc.HostAppVersion[1], desc.HostAppVersion[2], desc.HostAppVersion[3]));
//OutputMessage(String.Format(" Server NatNet Version: {0}.{1}.{2}.{3}", desc.NatNetVersion[0], desc.NatNetVersion[1], desc.NatNetVersion[2], desc.NatNetVersion[3]));
//checkBoxConnect.Text = "Disconnect";
if (desc.HostApp.Contains("TrackingTools"))
m_ServerToMillimeters = 1000.0f;
// [NatNet] [optional] send a test/response message
//OutputMessage("Sending TestRequest");
int nBytes = 0;
byte[] response = new byte[10000];
int rc = m_NatNet.SendMessageAndWait("TestRequest", out response, out nBytes);
if (rc == 0)
{
string str = " Server: " + System.Text.Encoding.ASCII.GetString(response, 0, nBytes);
//OutputMessage(str);
}
else
{
//OutputMessage(" Server: No Response.");
}
}
else
{
//OutputMessage("Error Connecting.");
//checkBoxConnect.Checked = false;
//checkBoxConnect.Text = "Connect";
}
}
private void Disconnect()
{
// [NatNet] disconnect
m_NatNet.Uninitialize();
//checkBoxConnect.Text = "Connect";
}
// [NatNet] m_NatNet_OnFrameReady will be called when a frame of Mocap data has is received
// from the server application.
void m_NatNet_OnFrameReady(NatNetML.FrameOfMocapData data, NatNetML.NatNetClientML client)
{
m_FrameQueue.Enqueue(data);
}
// Update is called once per frame
void Update () {
}
}
First of all: It's impossible to understand what you want to tell us with your questiontitle. It seems it's not related to the script, but to your dll you are using. Did you test your dll and imported it without errors? And you should initialize you variable "m_NatNet" at least with "null" or you will get warnings.
I'm not much of a programmer; sorry if I can't make myself very clear. How would I test the DLL? Also not sure how to initialize m_NatNet with null (though that doesn't seem to be causing the crash so it's not a top priority for me).
Answer by Bampf · Feb 07, 2011 at 09:05 PM
I'm guessing that you are getting an error on the line when it tries to construct NatNetML.FrameOfMocapData, or the line initializing the Queue of mocap data. (The other lines are simpler and seem unlikely to be the problem.)
To prove this, try removing the m_FrameOfData declaration and anything that uses it. If that doesn't do it, remove the queue as well.
If they are the problem, I suggest moving initialization of those variables into the Start function, and add exception handling around them. Maybe that way you'll get an error or exception, rather than a crash.
If you haven't already, make sure the DLL is somewhere in your project where Unity can find it. There's a discussion of that here: http://answers.unity3d.com/questions/36384/how-should-i-add-references-to-additional-mono-assemblies
Can't really say more without knowing something about NatNetDLL. You could be using it wrong, or it might not be installed correctly, or maybe there's a bug in Unity/Mono when trying to use this particular library.
EDIT: I found the following (unresolved) discussion where someone was getting a similar crash with NatNet in Mono: http://forum.naturalpoint.com/forum/ubbthreads.php?ubb=showflat&Number=42811 Might be worth following up with them. The unanswered question in that discussion is, does NatNet's standard example work in Mono (i.e. independent of Unity)? If not, that could be the problem. The crash certainly sounds similar.
It does seem to be a problem with that FrameOf$$anonymous$$ocapData line. Specifically, it only crashes when I include new NatNet$$anonymous$$L.FrameOf$$anonymous$$ocapData .The rest of the line doesnt cause a crash. If I do new the variable, it crashes whether its in start or in the code at the top. It seems clear that there's a problem with that particular line, but the very similar Queue-creating line (which also refers to NatNet$$anonymous$$L.FrameOf$$anonymous$$ocapData) does not cause the same crash. What else can I try? It seems like the only difference is the new part, but that code works in the original example.
If you're interested enough to download the NatNet files, they're available from http://www.naturalpoint.com/optitrack/support/downloads.html Scroll down to ARENA & Expression $$anonymous$$ocap Software -> look under the third bullet point ("Plugins, strea$$anonymous$$g and extras") and click the link ("Show... Natnet"). Download the zip file and the DLL file is in the lib folder. It comes with a user manual but it's very confusing for actually trying to implement the sofware.
FYI someone else seems to have run into a similar crash when using NatNet under $$anonymous$$ono. I added a link to my answer above.