The variable answerButtons of Quiz has not been assigned

I am working through the Quiz Master project in the 2D unity course and I’ve encountered an odd bug. I’m pretty sure I’ve done everything the tutorial video says to do and my project exhibits the expected behavior when I run it but it also throws out this error message:

UnassignedReferenceExeption: The variable answerButtons of Quiz has not been assigned.

This appears to be correct, I don’t assign a value to the variable in my code but I don’t think the video ever told me to. Can anyone shed some light on this?

Very likely the usual IDE issue. This may not be an issue in newer versions of the IDE packages that Unity provide but in the past we used to get this as a warning all the time. This is because the IDE is warning you that you are using a variable but you never gave it a value. We did give it a value in the inspector, but the IDE does not know this so it gives this warning.

If this is in fact the issue you could change your code to assign a default to the variable

[SerializeField] GameObject[] answerButtons = default;

This should remove the warning. If this is not the issue and the error persists, please share some code and we can take a look


Edit
Another option is to make it like the questions variable

[SerializeField] List<GameObject> answerButtons = new List<GameObject>();

Then you just need to update all the answerButtons.Length to answerButtons.Count and it will behave exactly the same in the code and inspector.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms