I am trying to access a script class (AimRotation) in my Awake() function to get user input variables and I only get a NullReferenceException. I made sure that the GameObjects are not null and that the AimRotation script is attached to the objects.
The frustrating part is that my script was already working perfectly fine but suddenly stopped working due to the exception. I don't recall changing anything in the code but I can't be 100% sure; only thing I can tell for sure is it happened after I was fiddeling around with the Animator (which is not accessing the GameObjects nor the AimRotation class). I am not sure what more information I can give.
EDIT: The NullReferenceException disappeared after restarting Unity. However I still don't know what threw the exception in the first place.
Here is some code:
Awake function in Controller:
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 class:
public class AimRotation : MonoBehaviour {
public float clampFactor = 0;
public int rotationOffset = 0;
public float getClampFactor()
{
return clampFactor;
}
public int getRotationOffset()
{
return rotationOffset;
}
}
RotationObject class:
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;
}
}