-1

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;
    }
}

enter image description here enter image description here

derHugo
  • 83,094
  • 9
  • 75
  • 115
SuperTasche
  • 479
  • 1
  • 7
  • 17

1 Answers1

3

You made an error in the name of your array when you get the AimRotation component

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()));
}
Hellium
  • 7,206
  • 2
  • 19
  • 49
  • 1
    You beat me to it lol. I think you should add a sentence though – Programmer Jun 28 '17 at 21:21
  • Hahaha. You won't believe it but this just saved my ass a ton of time. Somehow the NullReferenceException disappeared after I restarted Unity. However I was wondering why my rotation wasn't applied correctly. Thanks for that! ^^ – SuperTasche Jun 28 '17 at 21:26
  • 2
    You can accept answer if your problem is solved by this solution. – Programmer Jun 28 '17 at 21:57
  • A problem is solved. However I am still experiencing random NullReferenceExceptions at the same position in code. It's probably due to my bad code, but maybe someone knows more? – SuperTasche Jun 28 '17 at 23:47