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

Elijah Franklin
Elijah Franklin
882 Points

Game Object will not Consistently Destroy Itself.

I have a Scenario where I am trying to destroy game objects (said game objects will be refered to as "Mashrooms") with specific conditions. (There are also more than one of these Mashrooms). I have a line of code that triggers, setting a boolean from false to true on the Mashrooms with the required conditions telling it to destroy itself.

Here is the code that sets the boolean to true. (it's within an update loop)

GameObject G = GameObject.FindGameObjectWithTag("Mashroom"); 
MFG = G.GetComponent(); 
MFG.destroyed = true;

Here is the self destruct code for the Mashroom. (It is also on an update loop.)

if (destroyed == true && breed == true) 
{ Destroy(mashroomFullgrown); 
}

What happens is that everything will go according to plan the first one or two times., where the Mashrooms with the required conditions destroy themselves and everything works as expected. But after repeating this process more than once or twice, the Mashrooms fail to destroy themselves. What's worse is that Console doesn't have anything to say about this. A theory I have is that the Destroy command for the Mashroom I am using, destroys the asset, or the reference as well, severing the the connection between the two lines of codes. But that's the only lead I've got. If anyone has any ideas let me know. And also let me know if there is any further information that I can give that might have have to do with the problem.

Thanks!

P.S (MFG stands for MashroomFullgrown in this context.)