As far as I know, you can't detect collision without a script implementing OnCollisionEnter
.
You can attach the following script to all of your bricks which will dispatch/raise an event when something collided into it :
public class Brick : MonoBehaviour
{
public event System.EventHandler OnTouched;
void OnCollisionEnter(Collision collision)
{
if ( OnTouched != null )
OnTouched( this, System.EventArgs.Empty );
}
}
Then, supposing you have an array of Brick
s, you can subscribe to the OnTouched
event :
[SerializeField]
private Brick bricks ;
private void Start()
{
for( int brickIndex = 0 ; brickIndex < bricks.Length ; ++brickIndex )
bricks[brickIndex].OnTouched += OnBrickTouched;
}
private void OnBrickTouched( object sender, System.EventArgs e )
{
Brick brick = (Brick) sender;
// Do something with your brick
}
EDIT : After reading again your question, you want to detect collision on the ball. The principle is the same, check the scripts below.
public class BallEventArgs : EventArgs
{
public BrickScript Brick;
public BallEventArgs(string brick)
{
Brick = brick;
}
}
public class BallScript : MonoBehaviour
{
public event System.EventHandler<BallEventArgs> OnBrokenBrickTouched ;
void OnCollisionEnter(Collision collision)
{
Brick touchedBrick = collision.collider.GetComponent<Brick>();
if ( touchedBrick != null && touchedBrick.isBroken && OnBrokenBrickTouched != null )
OnBrokenBrickTouched ( this, new BallEventArgs(touchedBrick) );
}
}
// ........
public class GamePlayScript : MonoBehaviour {
public BallScript ballScript;
public BrickGroupsScript brickGroupsScript;
// Use this for initialization
void Start () {
ballScript.OnBrokenBrickTouched += OnBrokenBrickTouched ;
}
private void OnBrokenBrickTouched( object sender, BallEventArgs e )
{
BallScript ball = (BallScript) sender;
BrickScript brick = e.Brick;
// Do something with your ball and your brick
}
}