gameobject.sendmessage() doesn't actually send the message...

  • var BulletSpawn : Transform;
  • var Gun : Transform;
  • var Bullet : GameObject;
  • var Zombie : GameObject;
      • function Start () {
  • }
    • function Update () {
  • if(Input.GetButtonDown(“Fire1”)){
  • Shoot();
  • }
  • Network.Instantiate(Bullet, BulletSpawn.transform.position, BulletSpawn.transform.rotation, 0);
  • }
  • function Shoot(){
  • var hit: RaycastHit;
  • if (Physics.Raycast(transform.position, transform.forward, hit)){
  • if (hit.transform.tag == “Zombie”){
  • Debug.Log(“Got a hit!”);
  • Zombie = hit.transform.gameObject;
  • Zombie.SendMessage(“TakeDamage()”, “Sniper”);
  • }
  • }
  • }

That’s my code, and I do have the corrosponding “TakeDamage()” function call. Is it because my the sniper script (sending the message) is JS and my AI script is C#? (recieving the message). I’ve tried so many other things, but it doesn’t seem to work. I get the valid output from debug.log(“Got a hit!”);, but I can’t get the debug.log from the takedamage() function. Any help is greatly appreciated.

Instead of sending to the gameobject, get a reference to the component that you actually want to send it to.
Or take this opportunity to make the switch to full c#

How do I get a reference to the gameobject?

No, a reference to the Component (the script) that you want to send the message to.

I think this should work:

Zombie = hit.transform.gameObject;
Zombie.GetComponent(“script_name”).SendMessage(“TakeDamage()”, “Sniper”);

1 Like

I’m pretty sure when using send message you do:

SendMessage (“TakeDamage”…) you leave out the “()”.

1 Like

THANK YOU BOTH SO MUCH! I took the advice from seejayjames, and it didn’t work, and then I removed the extra set of parenthases () and it worked like a charm! Thank you so much!

Also, when I lower the field of view (For a scope), it’s too choppy. Is there a way I can fix that?