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