Phidgets

I am trying to get phidgets to work in unity for my university project, and my lecturer gave me the code he used a couple of years ago to do it.

The problem I am having is that while the transform.translate line is commented out the following code works, but if I leave that line in then when unity gets to that line, unity just freezes with the only way to unfreeze it being to close and re-open unity.

I have asked my lecturer about this and he didnt know what the problem was and neither do I.

using UnityEngine;
using System; 
using System.Collections;
using Phidgets;
using Phidgets.Events;
 
public class PhidgetTestScript : MonoBehaviour {
 void Start () {
 }
 void Update () {
  Accelerometer accel = new Accelerometer();
 
  accel.Attach += new  AttachEventHandler(accel_Attach); 
  accel.Detach += new DetachEventHandler(accel_Detach);
  accel.Error += new ErrorEventHandler(accel_Error);
 
  accel.AccelerationChange += new AccelerationChangeEventHandler (accel_AccelerationChange);
 
  accel.open();
 
  accel.waitForAttachment();
 
  accel.close();
 
  accel = null;
 }
 
 void accel_Attach(object sender, AttachEventArgs e){
 }
 
 void accel_Detach(object sender, DetachEventArgs e){
 }
 
 void accel_Error(object sender, ErrorEventArgs e){
 }
 
 void accel_AccelerationChange(object sender, AccelerationChangeEventArgs e){
 
  if (e.Acceleration > 0.5)
  {
   Debug.Log("left");
   //Translate the object along the X axis
   //transform.Translate(Vector3.up * Time.deltaTime, Space.World);
  }
  if (e.Acceleration < -0.5)
  {
   Debug.Log("Right");
   //Translate the object along the X axis
   //transform.Translate(Vector3.up * Time.deltaTime, Space.World);
  }
 }
}

Can I ask why you’re constructing the accelerometer every single frame? It seems like your application would be a lot faster if you constructed it once in Start, and then just gathered data in Update.

My thought is that you’ve added a new event handler every single frame, but you’re never de-registering the event handler, so you’d have so much events being called in one frame Unity would slow down to a halt.

I have done it like that as that is the way my lecturer told me to do it.

If what your saying is true, then wouldn’t unity also slow down to a halt (or atleast drasticly slow down) with the debug line? (which it isn’t doing).

Yes but you’re pointlessly allocating and deallocating ram on a constant basis. What you are in fact doing, is poor programming practise for game development.

In the case of a black box scenario where you need to make self contained code ie a plugin, what you are doing might make sense in non-performance critical applications.

If unity slows down or not has zero bearing on good or bad code, but everything to do with how powerful your particular computer is. In this example’s case I can only assume your lecturer is trying to teach you some fundamental concepts about c# rather than actually making good game accelerometer code - he is just using the accelerometer as an example in this case.

As I said in my first post, I need this for a university project. The project is making a game for either a mobile phone or one using phidgets. This code my lecturer gave me was the code he used a couple of years ago when he did it and it is for making a game using an accelerometer.

I understand what you mean by it being bad practise though, however isn’t what I said true? As in, if that was the problem then wouldn’t the dubug line also cause unity to slow down a lot?