public class bombscript : MonoBehaviour
{
public float fieldofImpact;
public float force;
public LayerMask LayerToHit;
public GameObject ExplosionEffect;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
OnCollisionEnter2D(Collision2D col);
{
explode();
}
}
void explode()
{
Collider2D[] objects = Physics2D.OverlapCircleAll(transform.position,fieldofImpact,LayerToHit);
foreach (Collider2D obj in objects)
{
Vector2 direction = obj.transform.position - transform.position;
obj.GetComponent<Rigidbody2D>().AddForce(direction * force);
}
GameObject ExplosionEffectIns = Instantiate(ExplosionEffect,transform.position,Quaternion.identity);
Destroy(ExplosionEffectIns,10);
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position,fieldofImpact);
}
void OnCollisionEnter2D(Collision2D col)
{
Debug.Log("OnCollisionEnter2D");
}
}
Not sure what line it's telling you the syntax error is on. The solution helper at the bottom usually says what line of code the error is on. On a side note, line 40 you're getting an argument for a collision2D but in the Debug you only have it printing a string. Maybe Debug.Log("OnCollisionEnter2D " + col); to show you what you collide with.
OnCollisionEnter2D() is a function that is called by Unity itself upon detecting that the object has collided with something, not a function that is called every time Update() is called. So you don’t need to call it in the Update() function.
Not sure what line it's telling you the syntax error is on. The solution helper at the bottom usually says what line of code the error is on. On a side note, line 40 you're getting an argument for a collision2D but in the Debug you only have it printing a string. Maybe Debug.Log("OnCollisionEnter2D " + col); to show you what you collide with.
– aktopshelf