2017-06-28 16 views
-1

私のAwake()関数のスクリプトクラス(AimRotation)にアクセスしてユーザー入力変数を取得しようとしていて、NullReferenceExceptionを取得するだけです。 GameObjectsがヌルでないことと、AimRotationスクリプトがオブジェクトにアタッチされていることを確認しました。Unity:GetComponentのNullReferenceException

私のスクリプトはすでに完璧に動作していましたが、例外のために突然動作を停止しました。私はコード内の何かを変更したことを思い出しませんが、私は100%確信することはできません。アニメーター(GameObjectsやAimRotationクラスにアクセスしていない)を使って試合をした後に、私が確かに言うことができるのは確かです。私はもっ​​と多くの情報を私に与えることはできません。

EDIT: Unityを再起動した後にNullReferenceExceptionが消えました。しかし、私はまだ最初に何が例外を投げたのか分からない。ここ

はいくつかのコードである:コントローラで

アウェイク機能:

protected List<RotationObject> rotObjs; 

void Awake() 
{ 
    rotObjs = new List<RotationObject>(); 

    GameObject[] rotationObjects = GameObject.FindGameObjectsWithTag("Rotation"); 
    GameObject[] rotationObjects2 = GameObject.FindGameObjectsWithTag("Rotation2"); 

    for (int i = 0; i < rotationObjects.Length; i++) 
    { 
     AimRotation aimRot = rotationObjects[i].GetComponent<AimRotation>(); 
     rotObjs.Add(new RotationObject(rotationObjects[i], aimRot.getClampFactor(), aimRot.getRotationOffset())); 
    } 

    for (int i = 0; i < rotationObjects2.Length; i++) 
    { 
     AimRotation aimRot = rotationObjects[i].GetComponent<AimRotation>(); 
     rotObjs.Add(new RotationObject(rotationObjects2[i], aimRot.getClampFactor(), aimRot.getRotationOffset())); 
    } 

} 

AimRotationクラス:

public class AimRotation : MonoBehaviour { 

    public float clampFactor = 0; 
    public int rotationOffset = 0; 

    public float getClampFactor() 
    { 
     return clampFactor; 
    } 

    public int getRotationOffset() 
    { 
     return rotationOffset; 
    } 
} 

RotationObjectクラス:

public class RotationObject 
{ 
    GameObject obj; 
    float clampFactor; 
    int rotationOffset; 

    public RotationObject (GameObject newObj, float newClampFactor, int newRotationOffset) 
    { 
     obj = newObj; 
     clampFactor = newClampFactor; 
     rotationOffset = newRotationOffset; 
    } 

    public GameObject getGameObject() 
    { 
     return obj; 
    } 

    public float getClampFactor() 
    { 
     return clampFactor; 
    } 

    public int getRotationOffset() 
    { 
     return rotationOffset; 
    } 
} 

enter image description here enter image description here

+0

可能な重複を取得するときに、あなたの配列の名前に誤りがなさ[何とNullReferenceExceptionある、と私はそれをどのように修正すればよいの?](HTTPS ://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Lestat

答えて

3

あなたがAimRotationコンポーネント

for (int i = 0; i < rotationObjects2.Length; i++) 
{ 
    AimRotation aimRot = rotationObjects2[i].GetComponent<AimRotation>(); // was rotationObjects[i] 
    rotObjs.Add(new RotationObject(rotationObjects2[i], aimRot.getClampFactor(), aimRot.getRotationOffset())); 
} 
+1

あなたは私にそれを叩いてくれます。私はあなたが文を追加する必要があると思うが、 – Programmer

+0

ハハハ。あなたはそれを信じませんが、これはちょうど私のお尻を時間のトンを助けました。何とかUnityを再起動した後、NullReferenceExceptionが消えました。しかし、なぜ私のローテーションが正しく適用されなかったのか不思議でした。ありがとう! ^^ – SuperTasche

+2

この解決策で問題が解決した場合は、回答を受け入れることができます。 – Programmer