Unassigned sprite in customer list

Hello!

I’m trying to make a game where you run a hospital, accept patients and treat them. The first thing you should do each morning is check a list of possible patients where you’ll get 4 new random patients each day. I’ve made a script that assigns a random sprite from a pool of possible sprites to each patient in the Awake method. This works fine if you just add a GameObject with this script attached to the scene.

Problem is, I’m not physically adding any patients here, just adding their information to a list that should display their name, symptoms and photo. Since no such GameObject exists in the scene, everytime I check the list I receive an Unassigned Reference Exception from the spriteRenderer variable in the patient script.

This makes sense, I understand where the error is coming from, but how could I possibly fix it?

Always the same: assign it or guard against it being unassigned.

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

By removing the requirement for a sprite renderer at this point, or changing it into a sprite holder for the time being until you arrange your objects to display those images.