前提・実現したいこと
古いものですがkinect v1で検出した特徴点のUnity上の座標を取得して表示したいです。
Kinect Wrapper Package内のKinectPointControllerのupdate内で
安直であるとは思うのですが特徴点の右肩、右ひじ、右手首のオブジェクトを
GameObject.Findを用いて取得しようとしたところ
NullReferenceException: Object reference not set to an instance of an object KinectPointController.Update () (at Assets/Assets/Script/Kinect/KinectModelControllers/KinectPointController.cs:149)
のエラーが出てしまいます。
ソースコードはUpdate内で以下のものを使用しています
if (GameObject.Find ("Shoulder_Right") && GameObject.Find ("Elbow_Right") && GameObject.Find ("Wrist_Right")) { Vector3 tmp1 = GameObject.Find ("Shoulder_Right").transform.position; x1 = tmp1.x; y1 = tmp1.y; z1 = tmp1.z; targetText1.text = tmp1.x.ToString()+","+tmp1.y.ToString()+","+tmp1.z.ToString(); Vector3 tmp2 = GameObject.Find ("Elbow_Right").transform.position; x1 = tmp2.x; y1 = tmp2.y; z1 = tmp2.z; targetText2.text = tmp2.x.ToString()+","+tmp2.y.ToString()+","+tmp2.z.ToString(); Vector3 tmp3 = GameObject.Find ("Wrist_Right").transform.position; x1 = tmp3.x; y1 = tmp3.y; z1 = tmp3.z; targetText3.text = tmp3.x.ToString()+","+tmp3.y.ToString()+","+tmp3.z.ToString(); } else if(GameObject.Find ("Shoulder_Right") == null||GameObject.Find ("Elbow_Right") == null||GameObject.Find ("Wrist_Right") ==null){ if (GameObject.Find ("Shoulder_Right")) { Vector3 tmp1 = GameObject.Find ("Shoulder_Right").transform.position; x1 = tmp1.x; y1 = tmp1.y; z1 = tmp1.z; targetText1.text = tmp1.x.ToString()+ "," + tmp1.y.ToString() + "," + tmp1.z.ToString(); } else { targetText1.text = "null"; //エラー箇所 } if (GameObject.Find ("Elbow_Right")) { Vector3 tmp2 = GameObject.Find ("Elbow_Right").transform.position; x1 = tmp2.x; y1 = tmp2.y; z1 = tmp2.z; targetText2.text = tmp2.x.ToString()+","+tmp2.y.ToString()+","+tmp2.z.ToString(); }else { targetText2.text = "null"; } if (GameObject.Find ("Wrist_Right")) { Vector3 tmp3 = GameObject.Find ("Wrist_Right").transform.position; x1 = tmp3.x; y1 = tmp3.y; z1 = tmp3.z; targetText3.text = tmp3.x.ToString()+","+tmp3.y.ToString()+","+tmp3.z.ToString(); }else { targetText3.text = "null"; } }
taragetTextはクラス内の最初にpublicで宣言しています
やはりGameObject.Findで参照しようとしている事が良くないのでしょうか
補足情報(FW/ツールのバージョンなど)
unity2017.4.8f1
kinect for Windows SDK1.8
kinect v1
元のKinectPointControllerのスクリプトも載せておきます
/* * KinectModelController.cs - Moves every 'bone' given to match * the position of the corresponding bone given by * the kinect. Useful for viewing the point tracking * in 3D. * * Developed by Peter Kinney -- 6/30/2011 * */ using UnityEngine; using System; using System.Collections; using UnityEngine.UI; public class KinectPointController : MonoBehaviour { //Assignments for a bitmask to control which bones to look at and which to ignore public enum BoneMask { None = 0x0, Hip_Center = 0x1, Spine = 0x2, Shoulder_Center = 0x4, Head = 0x8, Shoulder_Left = 0x10, Elbow_Left = 0x20, Wrist_Left = 0x40, Hand_Left = 0x80, Shoulder_Right = 0x100, Elbow_Right = 0x200, Wrist_Right = 0x400, Hand_Right = 0x800, Hip_Left = 0x1000, Knee_Left = 0x2000, Ankle_Left = 0x4000, Foot_Left = 0x8000, Hip_Right = 0x10000, Knee_Right = 0x20000, Ankle_Right = 0x40000, Foot_Right = 0x80000, All = 0xFFFFF, Torso = 0x10000F, //the leading bit is used to force the ordering in the editor Left_Arm = 0x1000F0, Right_Arm = 0x100F00, Left_Leg = 0x10F000, Right_Leg = 0x1F0000, R_Arm_Chest = Right_Arm | Spine, No_Feet = All & ~(Foot_Left | Foot_Right), UpperBody = Shoulder_Center | Head|Shoulder_Left | Elbow_Left | Wrist_Left | Hand_Left| Shoulder_Right | Elbow_Right | Wrist_Right | Hand_Right } public SkeletonWrapper sw; public GameObject Hip_Center; public GameObject Spine; public GameObject Shoulder_Center; public GameObject Head; public GameObject Shoulder_Left; public GameObject Elbow_Left; public GameObject Wrist_Left; public GameObject Hand_Left; public GameObject Shoulder_Right; public GameObject Elbow_Right; public GameObject Wrist_Right; public GameObject Hand_Right; public GameObject Hip_Left; public GameObject Knee_Left; public GameObject Ankle_Left; public GameObject Foot_Left; public GameObject Hip_Right; public GameObject Knee_Right; public GameObject Ankle_Right; public GameObject Foot_Right; private GameObject[] _bones; //internal handle for the bones of the model //private Vector4[] _bonePos; //internal handle for the bone positions from the kinect public int player; public BoneMask Mask = BoneMask.All; public float scale = 1.0f; // Use this for initialization void Start () { //store bones in a list for easier access _bones = new GameObject[(int)Kinect.NuiSkeletonPositionIndex.Count] {Hip_Center, Spine, Shoulder_Center, Head, Shoulder_Left, Elbow_Left, Wrist_Left, Hand_Left, Shoulder_Right, Elbow_Right, Wrist_Right, Hand_Right, Hip_Left, Knee_Left, Ankle_Left, Foot_Left, Hip_Right, Knee_Right, Ankle_Right, Foot_Right}; //_bonePos = new Vector4[(int)BoneIndex.Num_Bones]; } // Update is called once per frame void Update () { if(player == -1) return; //update all of the bones positions if (sw.pollSkeleton()) { for( int ii = 0; ii < (int)Kinect.NuiSkeletonPositionIndex.Count; ii++) { //_bonePos[ii] = sw.getBonePos(ii); if( ((uint)Mask & (uint)(1 << ii) ) > 0 ){ //_bones[ii].transform.localPosition = sw.bonePos[player,ii]; _bones[ii].transform.localPosition = new Vector3( sw.bonePos[player,ii].x * scale, sw.bonePos[player,ii].y * scale, sw.bonePos[player,ii].z * scale); } } } } }
追記
エラー自体は出なくなりましたがGameObject.Findでオブジェクトが見つかりません。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/16 11:53
2018/11/17 04:43
2018/11/19 07:11