# SendMessage HELP

**URL:** https://discussions.unity.com/t/sendmessage-help/162018
**Category:** Questions & Answers
**Created:** [March 31, 2016, 6:20am UTC](https://discussions.unity.com/t/sendmessage-help/162018 "2016-03-31T06:20:15Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![carlqwe](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@carlqwe](https://discussions.unity.com/u/carlqwe)
#### Post date: [March 31, 2016, 6:20am UTC](https://discussions.unity.com/t/sendmessage-help/162018/1 "2016-03-31T06:20:15Z")

</div>

Im trying to acess a script on a gameobject that is called and tagged as “Enemy”. But Sendmessage doesnt work for some reason i’ve tried getting the script component, but it doesnt work because there will be more than one “Enemy” in the scene at the same time.

To make it clear, i have a script that is suppose to “Send Message” To a function called “ApplyDamage”.  
That is attached to about 20 “Enemies” in the scene. And to be sure that the enemy i hit will “take damage”  
i’ve got a “hit.transform” to support that.

I’ve had some help from Debug.Log, and it seems like the problem is the “SendMessage” Line in the code. In the console i get “Suppose to send message” but never “TookDamage”.  
Please help im stuck here and dont know what to do!  
appreciate it, Bye

Script\_1:

```
if(hit.transform.gameObject.tag == "Enemy")
			{ 
				Debug.Log("HIT THE ENEMY");
				var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
				Destroy(particleClone.gameObject, 0.3);
				Debug.Log("SPAWNED PARTICLES");
				
				//SendMessage GETTING ERROR HERE!!
				hit.transform.SendMessage("ApplyDamage", SendMessageOptions.DontRequireReceiver);
				Debug.Log("Suppose to send message");
			}

```

Script\_2:

```
    function ApplyDamage()
    {
    	Health -= 50;
    	Debug.Log("TookDammage");
    }

```

---

<div class="post-metadata">

### Author: ![Eudaimonium](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/eudaimonium/32/472492_2.png) [@Eudaimonium](https://discussions.unity.com/u/Eudaimonium)
#### Post date: [March 31, 2016, 10:40am UTC](https://discussions.unity.com/t/sendmessage-help/162018/2 "2016-03-31T10:40:25Z")

</div>

Replace Send Message bit with this:

```
                 // Your "Script2" is whatever your actual script name is
                 hit.transform.gameObject.GetComponent<Script2>().ApplyDamage();
                 Debug.Log("Suppose to send message");

```

Or, for more robustness if you acidentally have an enemy without Enemy tag, or something without enemy script is wrongly assigned Enemy tag:

```
Script2 script = hit.transform.gameObject.GetComponent<Script2>();
if (script != null)
{
  script.ApplyDamage();
}

```

---

<div class="post-metadata">

### Author: ![phxvyper](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@phxvyper](https://discussions.unity.com/u/phxvyper)
#### Post date: [March 31, 2016, 8:46pm UTC](https://discussions.unity.com/t/sendmessage-help/162018/3 "2016-03-31T20:46:38Z")

</div>

The reason why SendMessage isn’t working is because it can’t find a function ApplyDamage that has a parameter.

If you wanted to call SendMessage with the SendMessageOptions parameter, you’d have to call it like so:

```
SendMessage("ApplyDamage", null, SendMessageOptions.DontRequireReceiver);

```

* * *

However, You’re calling it like so:

```
SendMessage("ApplyDamage", SendMessageOptions.DontRequireReceiver);

```

which is **identical to** :

```
SendMessage("ApplyDamage", SendMessageOptions.DontRequireReceiver, 
    SendMessageOptions.RequireReceiver);

```

If you want to pass the SendMessageOptions parameter, then you **must** call it with all three parameters. If you don’t care about the SendMessageOptions parameter, then you only need the function name. **Keep in mind that in this case, you should use the “No options” version of calling the function.**

```
SendMessage("ApplyDamage"); // No options

SendMessage("ApplyDamage", null, SendMessageOptions.DontRequireReceiver); // With options.

```

Please refer to the [function’s documentation.](http://docs.unity3d.com/ScriptReference/GameObject.SendMessage.html)
