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
Apologies if I am wrong with this but when you say Debug.Log is working, it's displaying the name of the objects you are hitting, because I thought you would have needed: hit.gameObject.name Which would also lead me to believe that you need hit.gameObject.SendMessage (and so On..) At the moment, it looks like you are sending the message to itself, which is pointless. Hope this helps in some way.
– MrSteve1I had this bug too, and i've simply just changed SendMessage for object refferences
– DajBuziat MrSteve41 Hmm yea.. it does actually only debug itself, and send the message to itself apparently. But when i try to add hit.gameobject it just says that raycast2dhit[] doesnt contain a definition for sendmessage, so idk whats going on there. Other than i might have wrote the function inverted or something, idk and at DajBuzi, what do you mean with you changed sendmessage for object references?
– bob1234When you call 'gameObject' it actually means the same as 'this.gameObject'. You should call hit.gameObject instead
– IvovdMarelOh okay, thanks. But as i wrote above, that doesnt work
– bob1234