###質問内容
UnityのC#スクリプトで、親オブジェクトから子オブジェクトを全4つ取得し、その子オブジェクトにHinge JointをAddComponent したいのですが、HingeJointの設定がうまくいきません
どのように直せばうまく動くか教えていただけると助かります。
※追記
親オブジェクトも子オブジェクトもプレハブを用いています。親オブジェクトはEmptyで、子オブジェクト
はCubeです。CubeにはHingeJointのスクリプトの他に、力を加えるスクリプト(AddForce)をComponentしています。
###発生している問題・エラーメッセージ
Assets/Script/Alpha.cs(34,16): error CS1061: Type UnityEngine.HingeJoint' does not contain a definition for
Anchor' and no extension method Anchor' of type
UnityEngine.HingeJoint' could be found (are you missing a using directive or an assembly reference?)
Assets/Script/Alpha.cs(35,16): error CS1061: Type UnityEngine.HingeJoint' does not contain a definition for
Axis' and no extension method Axis' of type
UnityEngine.HingeJoint' could be found (are you missing a using directive or an assembly reference?)
Assets/Script/Alpha.cs(36,16): error CS1061: Type UnityEngine.HingeJoint' does not contain a definition for
ConnectedBody' and no extension method ConnectedBody' of type
UnityEngine.HingeJoint' could be found (are you missing a using directive or an assembly reference?)
###該当のソースコード
C#
using UnityEngine;
using System.Collections;
public class Alpha : MonoBehaviour
{
void Start() { int count = 0; GameObject[] childObject = new GameObject[4]; //Search child objects foreach (Transform child in gameObject.transform) { childObject[count] = gameObject.transform.FindChild(child.name).gameObject; count++; } AttachHingeJoint(childObject[0], childObject[1], childObject[2], childObject[3]); } public void AttachHingeJoint(GameObject block0, GameObject block1, GameObject block2, GameObject block3) { //Connect 1&0 block1.AddComponent<HingeJoint>(); HingeJoint Hinge1 = block1.GetComponent<Rigidbody>().GetComponent<HingeJoint>(); block1.GetComponent<Rigidbody>(); Hinge1.Anchor = new Vector3(0.5f, 0.0f, 0.5f); Hinge1.Axis = new Vector3(0.0f, 1.0f, 0.0f); Hinge1.ConnectedBody = block0.GetComponent<Rigidbody>(); }
}
###試したこと
エラーは
Hinge1.Anchor = new Vector3(0.5f, 0.0f, 0.5f);
Hinge1.Axis = new Vector3(0.0f, 1.0f, 0.0f);
Hinge1.ConnectedBody = block0.GetComponent<Rigidbody>();
の3行です。
子オブジェクトの呼び出しがうまくいっていないのかと思い、printさせたら、取得はできているみたいでした。
また、HingeJointをAddComponentするだけのクラスを作ってみましたが、そちらもエラーが出ませんでした。
子オブジェクトの呼び出し+HingeJointの組み合わせが悪いのでしょうか?
###補足情報(言語/FW/ツール等のバージョンなど)
Unityのバージョンは5.3.7、エディターはVisual Studio Community 2017 for Macを使用しています
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/22 08:26
2017/12/22 08:32
2017/12/22 09:34
2017/12/26 07:47