Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development

Derek Briggs
Derek Briggs
2,817 Points

MissingReferenceException when spawning flies.

My Flyspawner Class works perfectly and will spawn flies when the game starts. But, after adding the line "FlySpawner.totalFlyCount--;" to my FlyPickup class, I get an exception. Unity tells me that i'm trying to reference a deleted gameObject.

Derek Briggs
Derek Briggs
2,817 Points

I fixed it. I selected the Fly in the hierachy window. I hit the "apply" button towards the top of the inspector to update the Fly prefab. Then i dragged the prefab into the inspector from the prefab folder instead of the hierachy.

2 Answers

Alan Mattanó
PLUS
Alan Mattanó
Courses Plus Student 12,188 Points

Is there a lost reference "None (GameObject)" in the inspector ? If so drag the correct game object (Fly prefab) into the correct box variable in the inspector. Or look for the code reference.

Derek Briggs
Derek Briggs
2,817 Points

I dragged the fly from the hierachy into the inspector. The game spawns 12 flies when the game starts. But after a fly is caught it throws the error and no new flys spawn. the error thrown is "MissingReferenceException". It says "the object of type GameObject has been destroyed but you are still trying to access it".

Derek Briggs
Derek Briggs
2,817 Points

This answer made me think of trying the prefab from the prefab folder instead of the hierachy. thanks.

Alan Mattanó
PLUS
Alan Mattanó
Courses Plus Student 12,188 Points

It can be also a Array [number] problem. You are trying to access to a "number" in the array that do not exist?