How would I handle the lose collider with multiple balls? One of the main concerns that I see happening is the amount of balls in the game. When the last ball collides with the lose collider, then they should lose. So the game would have to keep track of how many balls are in the game. How would I go about doing this?
I would look at doing the same way that you keep track of the number of breakable bricks you could use a static ballCount variable in the Ball class.
When you add a new ball increment the counter when you destroy a ball because it has hit the lose collider reduce the counter. When the counter is <= 0 then you have lost the last ball.
Remember to update levelManager to set the ballcount back to zero on loadlevel and loadnextnevel.
I thought about that already. So firstly I would have to make the balls breakable, and destroy them on collision with the lose collider, no big deal, just that when they hit the collider, the same would go for when they hit bricks! So my thoughts are that the ball is not a breakable and the on collision would mean on any collision, not just collision with the lose collider. So if the ball hits a brick it would do the same thing to the ball. Very confusing.
No need for any of that I don’t think. Just thinking out loud.
The LoseCollider would need a private int ballCount;
and
public void AddBall() {
ballCount++;
}
in the Start() method it would need ballCount = 0;
in the OnTriggerEnter2D(Collider2D collider) method
ballCount–;
Destroy(collider);
if (ballCount <= 0) {
//trigger lose condition
}
Then when you Invoke a new ball object you would need to call LoseCollider.AddBall();
I’ll give this a try when I get back from work today! Thanks for the time taken to help a beginner!
Back on my crusade against global states (i.e.: static variables), they’re a bad habit in general and should be used only when really necessary. Especially if you’re starting to learn designing and coding, it’s better not to get used to such bad habits. 
You can just setup a game object container (Ball Container), which will have all the ball game objects as children, and every time a ball triggers the lose collider, you make a check if there’re no more children in the container objects, if yes, the player loses, if not, the game goes on.
To do that, you can use the
field.
No more ugly statics wandering around your code. 
Good idea. Just make sure when you invoke new balls you assign them to the Ball Container…
Off work, I actually followed what you had mentioned previously because it kind of fell in line with what has been taught throughout the course. Unfortunately I don’t really understand this transform.childcount thing. Is there any way that I could get a step by step? I am currently trying blind and will see where this leads me. I’ve been trying to experiment to see how I can add new things to the game like bigger paddles, multiple paddles, multiple balls, bigger balls. Thanks.
So far
1 - open ball script, add public int ballCountainer;
2 - created an empty gameobject and named it Ball Container. Dragged the ball into it. (this is the game object container right?)
3 - open lose collider script and somewhere in the OnTriggerEnter2D trigger have it reference BallContainer for a count but thats where I am lost.
With the bricks it was kinda easier because of the tags, we just said reference the breakables, and brick is breakable, so every instance makes the brick count ++ and on collision brick count – just dont know how to do that without tags or how to make it ask ballcontainer for its count.
Ok, I think the plan would be to create an empty gameobject and add a ballcontroller script to it.
in the script add…
public GameObject ballPrefab;
public void InvokeNewBall(vector3 paddlePosition) {
GameObject ball = Invoke(ballPrefab, paddlePosition, Quaternion.identity) as GameObject;
ball.parent = this; // make the ball spawn as a child of the ballController gameobject
}
public int GetNumberOfBalls() {
return transform.childCount;
}
In the losecollider script
private BallController ballController;
void Start() {
ballController = GameObject.GetComponent < BallController >();
}
void OnTriggerEnter2D(Collider2D ball) {
Destroy(ball);
if (ballController.GetNumberOfBalls() <= 0) {
DoEndGame();
}
}
When ever you wanted to add a new Ball you would need to use grab the ballcontroller script and use the InvokeNewBall method passing your paddles position.
Probably inside your paddle script;
private BallController ballController;
void start() {
ballController = GameObject.GetComponent < BallController >();
ballController.InvokeNewBall(paddle.transform.position + Vector3.up * ballOffset);
}
Please bear in mind that I’m just typing this into the browser. I’ve not got Unity or Monodevelop running so the code example probably won’t run but it might give you and idea of where to look…
I really hope that helps.
Your ideas are ok, with just a little bit “too many” things in it. 
It’s actually way simpler to implement this design.
- Create the Ball Container game object.
- Inside it, put the Ball game object (and, if needed, future instances of new balls created at runtime)
- Open the LoseCollider script, and add this code:
[code]public Transform ballContainer;
void LateUpdate(){
if (ballContainer.childCount <= 0) {
//write “lose” code in here
}
}
void OnTriggerEnter2D (Collider2D collider) {
Destroy (collider.gameObject);
}[/code]
- In the Inspector, feed the Ball Container game object to the ballContainer field in the script
That’s it. 
Btw, you need to use the LateUpdate method in the script, since the childCount is NOT updated before the end of OnTriggerEnter2D method, to check it just add a Debug.Log(ballContainer.childCount) before and after the Destroy, and you’ll see that you’ll get the same number in the console.
Hint: you could easily use this solution for the bricks too, try to do this as a challenge!
That Worked. Thank you very much. I’m going to be using the lateupdate in later code too, for the brick destroyed sound that keeps getting cut off on the last brick in a level. Thanks alot.
By the way, this should probably be added to the course. I think I learned a good bit just by watching it get put together!
I am having trouble following where this ballContainer comes from. Does your method involve creating a whole new script or just by manipulating the inspector? My game will run, but does not recognize when there are no balls left.