2016-10-17 3 views
0

두 스크립트는 계층 구조의 동일한 빈 GameObject에 연결됩니다. 먼저 SpawnObjects 스크립트가 MoveObject에 연결됩니다.왜 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

게재 위치입니다. 스폰 된 객체에 액세스하려는 구성 요소가 연결되지 않은 가능성 그래서 항상 그것을 사용하기 전에 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; 
관련 문제