親オブジェクトからinstantiateで子オブジェクトのcloneを作っているんですが
その子オブジェクトのスクリプトから親側の値を参照しようとするとエラーになってしまいます。
親からinstantiate製の子オブジェクトにはどうやって値を渡せばいいのでしょうか?```lang-<C#>
コード
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public GameObject[] fireObjectList; public bool destroy = false; void Update () { Transform goFire = transform.Find ("Muzzle"); if (Input.GetButtonDown ("Fire1")) { foreach (GameObject fireObject in fireObjectList) { GameObject go = Instantiate (fireObject, new Vector3 (goFire.position.x, goFire.position.y, 0), Quaternion.identity) as GameObject; go.transform.parent = transform; } if (Input.GetButtonUp ("Fire1")) { destroy = true; } else { destroy = false; } } }
}
```lang-<C#> コード using UnityEngine; using System.Collections; public class FireObject : MonoBehaviour { Player player; void Awake () { player = GetComponent<Player> (); } // Update is called once per frame public void Update () { if (player.destroy) { Destroy (this.gameObject); } } }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/06/13 08:35