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

C# C# Objects Loops and Final Touches Playing the Game

Jamie Wyton
Jamie Wyton
3,011 Points

Why am I winning?!

I have compiled my program and executed it to the result of 'Won', so I've done everything right so far, but there's a part of the program that's not making sense to me.

In the Level class where // each tower can fire upon invaders, we go through a foreach loop on every tower, each time calling on the FireOnInvaders method. The argument passed on in the parenthesis is _invaders to which end becomes invaders in the method being called. In the Tower class we go through another foreach loop going through every invader and then if that invader in the loop is active and in range of the tower then the invader.DecreaseHealth method is called.

Here is my problem, its only the instance of the array of invaders created when the FireOnInvaders was called that's been affected. Not the actual array of _invaders that belong to the Level class.

Right?

1 Answer

Steven Parker
Steven Parker
229,732 Points

The name of an array is a reference to the array.

You don't get a new instance (or copy) of an array just by naming it as a parameter. You get a reference to the same array.