私はボールゲームのようなゲームを持っています。私のゲームでは、gameManagerがgameManagerであり、そのgameManagerでボールをインスタンス化します。私のボールが動力を持ってボールを飛ばして別のスクリプトにある数学配列に材料を追加すると、そのスクリプトにアクセスしようとすると、というオブジェクトがインスタンス化されていませんと言います。ここでプレハブスクリプトにアクセスできない:NullReference例外
は、私がアクセスもしたいスクリプトが私のボールプレハブにattechedさMoveBallスクリプトです、私のボールPREFAは、プレーヤーのタグを持っている
public class PowerHit : MonoBehaviour {
MoveBall moveBall;
Renderer rend;
private Material yourMaterial;
// Use this for initialization
void Start() {
moveBall = GameObject.FindWithTag ("Player").GetComponent<MoveBall>();
rend = moveBall.GetComponent<Renderer>();
yourMaterial = (Material)Resources.Load("Tennis",typeof(Material));
}
void OnCollisionEnter(Collision other)
{
Debug.Log (moveBall);
if (other.gameObject.tag == "Player") {
moveBall.mats [1] = yourMaterial;
}
}
}
をやったことです。
だから...どの行がエラーですか? 'moveBall.mats [1] = yourMaterial;'? – Serlite
'moveBall'はnullではありませんか? **正確**エラーメッセージとは何ですか? – Hellium
NullReferenceException:オブジェクト参照がオブジェクトのインスタンスに設定されていません PowerHit.Start()(Asset/PowerHit.cs:12) –