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 Simon 9 · Feb 07, 2011 at 05:07 PM · gameobjectcrash

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 () {

 }

}

Comment
Add comment · Show 2
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 Bunny83 · Feb 07, 2011 at 05:18 PM 0
Share

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.

avatar image Simon 9 · Feb 09, 2011 at 05:43 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 3 · 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 Simon 9 · Feb 09, 2011 at 05:20 PM 0
Share

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.

avatar image Simon 9 · Feb 09, 2011 at 05:20 PM 0
Share

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.

avatar image Bampf · Feb 10, 2011 at 05:35 PM 0
Share

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.

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

No one has followed this question yet.

Related Questions

Easy collision script not working, don't understand. 2 Answers

Set variable in script from a different script... 3 Answers

Disable/Enable Script or Add/Remove? 1 Answer

Change position of GameObject between unity3d and eclipse 0 Answers

How to change variables on a new gameobject's script 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