2016-10-17 6 views
0

両方のスクリプトは、階層内の同じ空のGameObjectにアタッチされます。 まず、SpawnObjectsスクリプトがMoveObjectsに添付されます。なぜGetComponentにしようとするとnull例外が発生しますか?

これは例外を除いてスクリプトです。 例外は、ライン上にある:

mover.minXPos = minXPos; 

例外メッセージ:

NullReferenceException: Object reference not set to an instance of an object 
SpawnObjects.RandomSpawn() (at Assets/MyScripts/SpawnObjects.cs:28) 
SpawnObjects.Start() (at Assets/MyScripts/SpawnObjects.cs:18) 

マイコード:

using UnityEngine; 
using System.Collections; 

public class SpawnObjects : MonoBehaviour { 

    public GameObject PrefabToSpawn; 
    public int MaximumObjects = 100; 
    public int minXPos = -1000; 
    public int maxXPos = 1000; 
    public int minYPos = 50; 
    public int maxYPos = 150; 
    public int minZPos = -1000; 
    public int maxZPos = 1000; 

    // Use this for initialization 
    void Start() { 

     RandomSpawn(); 
    } 

    private void RandomSpawn() 
    { 
     for (int i = 0; i < MaximumObjects; i++) 
     { 
      Vector3 spawnLocation = new Vector3(Random.Range(minXPos, maxXPos), Random.Range(minYPos, maxYPos), Random.Range(minZPos, maxZPos)); 
      GameObject spawned = (GameObject)Instantiate(PrefabToSpawn, spawnLocation, Quaternion.identity); 
      MoveObjects mover = spawned.GetComponent<MoveObjects>(); 
      mover.minXPos = minXPos; 
      mover.maxXPos = maxXPos; 
      mover.minYPos = minYPos; 
      mover.maxYPos = maxYPos; 
      mover.minZPos = minZPos; 
      mover.maxZPos = maxZPos; 
     } 
    } 
} 

そして、これがMoveObjectsのスクリプトです

using UnityEngine; 
using System.Collections; 

public class MoveObjects : MonoBehaviour { 

    public int minXPos = -1000; 
    public int maxXPos = 1000; 
    public int minYPos = 50; 
    public int maxYPos = 150; 
    public int minZPos = -1000; 
    public int maxZPos = 1000; 
    public float speed = 30; 

    private Vector3 destinationLocation; 

    private float midX; 
    private float midY; 
    private float midZ; 

    void Start() 
    { 
     midX = (minXPos + maxXPos)/2; 
     midY = (minYPos + maxYPos)/2; 
     midZ = (minYPos + maxYPos)/2; 
     GenerateNewDestinationPoint(); 
    } 

    void Update() 
    { 
     Move(); 
     if (ArrivedAtLocation()) 
      GenerateNewDestinationPoint(); 
    } 

    private void Move() 
    { 
     transform.LookAt(destinationLocation); 
     transform.Translate(transform.forward * speed * Time.deltaTime); 
    } 

    private bool ArrivedAtLocation() 
    { 
     return (Vector3.Distance(transform.position, destinationLocation) < 1); 
    } 

    private void GenerateNewDestinationPoint() 
    { 
     float newX = (transform.position.x < midX) ? Random.Range(midX, maxXPos) : Random.Range(minXPos, midX); 
     float newY = (transform.position.y < midY) ? Random.Range(midY, maxYPos) : Random.Range(minYPos, midY); 
     float newZ = (transform.position.z < midZ) ? Random.Range(midZ, maxZPos) : Random.Range(minZPos, midZ); 

     destinationLocation = new Vector3(newX, newY, newZ); 
    } 
} 
+0

なぜCタグですか? –

答えて

1

これはposですSpawnされたオブジェクトにアクセスしようとしているコンポーネントが接続されていないことを確認します。したがって、使用する前にnullをチェックするのは常に安全です。

MoveObjects mover = spawned.GetComponent<MoveObjects>(); 
    if(mover == null) 
    { 
     // your prefab doesn't have the component attached. maybe add it. 
     mover = spawned.AddComponent<MoveObject>(); 
    } 
    mover.minXPos = minXPos; 
    mover.maxXPos = maxXPos; 
    mover.minYPos = minYPos; 
    mover.maxYPos = maxYPos; 
    mover.minZPos = minZPos; 
    mover.maxZPos = maxZPos; 
関連する問題