Life System in Block Breaker

Hi all,

I have just finished up the Block Breaker section of the course, and I wanted to add a life system to my game. I mostly understood what was going on when following the course, but its much harder when you are on your own!

Anyway, I have managed to add the life system to the game, but what I am having trouble with is resetting the ball to the paddle. So far in my LoseCollider.cs script, I have this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LoseCollider : MonoBehaviour
{
    public int playerLives = 3;
    public Ball ball;
    public Paddle paddle;
    public Vector2 paddleToBallVector;
 
    private void OnTriggerEnter2D(Collider2D collision)
    {
        playerLives--;
        if (playerLives > 0)
        {
            ball = GameObject.FindObjectOfType<Ball>();
            paddle = GameObject.FindObjectOfType<Paddle>();
            ball.transform.position = new Vector3(paddle.transform.position.x, 1.343f);      
        }
        else if (playerLives <= 0)
        {
            SceneManager.LoadScene("End Game");
        }
    }
}

my initial thoughts were adding “Ball.hasStarted = False” within the private void OnTriggerEnter2D(Collider2D collision) section, but the hasStarted is underlined in red with the error “Ball.hasStarted” is inaccessible due to its protection level.

Anyone able to point out where im going wrong and tell me how I can reset the ball to the paddle!

Thanks in advance.

been a long time since I played with Break Breaker, do you have a git repo I can pull it from?

I do not unfortunately. Ive never used github or anything like that before, so I can get you one, but it might take me a while to work out how!

Code

Summary
 public void Lose_Collider () // Handles reseting the ball and player death.
    {
        if (Player_Lives > 1)
        {
            Player_Lives--;
            Game_State.DisplayPlayerLives();
            FindObjectOfType<Ball>().Reset_Ball();
        }
        else
        {
            SceneManager.LoadScene(Level);
        } 
       
 public void Reset_Ball()
    {
        LaunchedBall = false;


        LockBallToPaddle();

    }
 public void LockBallToPaddle()
    {


        Vector3 PaddlePosition = new Vector3(Paddle_1.transform.position.x, Paddle_1.transform.position.y, Paddle_1.transform.position.z);
        transform.position = PaddlePosition + PaddleToBallVector;

    }

Apologizes if it isn’t much help.

Uh I think this is right?

https://github.com/phayilgaming/Block-Breaker-2.0.git

have you considered creating a method in Ball to reset the ball’s position and then calling that method from LoseCollider?

Summary
public class LoseCollider : MonoBehaviour
{
  private void OnTriggerEnter2D(collider2D collision)
  {
  playerLives--;
  if (playerLives > 0) { GameObject.FindObjectOfType<Ball>().ResetPosition(); }
  else { SceneManager.LoadScene("End Game"); }
  }
}
public class Ball : MonoBehaviour
{
  public void ResetPosition()
  {
  // your code here
  }
2 Likes

This worked! Thank you very much!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms