Domanda

I have successfully connected a bluetooth device to unity and trying to use the read in data to control other game object with this as reference http://answers.unity3d.com/questions/16484/how-to-import-neurosky-mindset-data-into-unity.html

I attached the connection script to an empty game object and it can read in some variables. I set all variable to be public int and include the connection script to another game object script

public s_ReadNeuro readNeuroScript;

The problem is after that, I do not know how to get the public variable from the connection script (for example, the attention and meditation values) which is read in constantly. How can I get them and use it in another game object script?Thank you

Here is the connection script attached to an empty game object

using UnityEngine;
using System.Collections;

public class s_ReadNeuro : MonoBehaviour {


public int tgHandleId;
public int tgConnectionStatus;
public int tgPacketCount;
public float attention;
public float meditation;
// Use this for initialization
void Start () {
   setupNeuro();
}

// Update is called once per frame
void Update () {
   readNeuro();
}


void setupNeuro() {
    tgHandleId = ThinkGear.TG_GetNewConnectionId();

    tgConnectionStatus = ThinkGear.TG_Connect(tgHandleId,
                                     "\\\\.\\COM4", 
                                     ThinkGear.BAUD_9600, 
                                     ThinkGear.STREAM_PACKETS);

}


void readNeuro() {
    tgPacketCount = ThinkGear.TG_ReadPackets(tgHandleId, -1);

    attention = ThinkGear.TG_GetValue(tgHandleId,  ThinkGear.DATA_ATTENTION);

    meditation = ThinkGear.TG_GetValue(tgHandleId, ThinkGear.DATA_MEDITATION);

}}
È stato utile?

Soluzione

There are basically two ways of doing this. Either wire your OtherGameObject with the s_ReadNeuro game object in the editor or find the s_ReadNeuro game object in the OtherGameObject using Unity's find functions. Which one to use depends on your use case, but in general I prefer wiring in the editor to using the Find functions (less code, less hassle). In any case your OtherGameObject would look something like this:

class OtherGameObject : MonoBehaviour {

    public s_ReadNeuro readNeuroInstance;


    void Update() {
        var attention = readNeuroInstance.attention;

        // do something with it.
    }

}

Then in the editor, create a new game object, attach the OtherGameObject behaviour to it and then drag the instance of the GameObject having the s_ReadNeuro script on it to the "Read Neuro Instance" field in the inspector of the OtherGameObject.

If you want to use the find methods, extend the script of OtherGameObject as follows:

class OtherGameObject : MonoBehaviour {

    private s_ReadNeuro readNeuroInstance;


    void Start() {
          readNeuroInstance = GameObject.FindObjectOfType(typeof(s_ReadNeuro));
    }

    void Update() {
        var attention = readNeuroInstance.attention;

        // do something with it.
    }

}

In this case you don't need to wire the objects in the editor. Be sure to call the Find function in Start or Awake as it's not exactly a cheap function to call.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top