So i got the CircleCastAll to work, it debug.log’s nicely with what it collides with. I just have an error, i’ve added a sendmessage to the for loop, but when it collides with an object it says DamageTaken has no reciever. And i have a function called DamageTaken in another script, it’s public ofcourse. What i need the script to do, is to send the damage message to my other script, but why isnt it working?
My code is as follows:
Grenade.cs
public Transform pointOfCircle;
public float radius = 0.5f;
public LayerMask whatIsDestroyable;
public float distance = 1f;
void Update()
{
RaycastHit2D[] hit;
hit = Physics2D.CircleCastAll (pointOfCircle.position, radius, new Vector2 (1, 1), distance, whatIsDestroyable);
for( int i = 0; i < hit.Length; i++)
{
Debug.Log("gameobject name " + gameObject.name + " hit");
gameObject.SendMessage("DamageTaken", 1, SendMessageOptions.RequireReceiver);
}
}
and
TileHealth.cs
public int health = 3;
public void DamageTaken(int damage)
{
if(health > 0)
{
health -= damage;
}
if(health <= 0)
{
health = 0;
Destroy (gameObject);
}
}
Help appreciated, thanks in advance