Search Unity

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

Discussion in 'Scripting' started by The-Game-Master, Aug 30, 2014.

  1. The-Game-Master

    The-Game-Master

    Joined:
    Aug 6, 2014
    Posts:
    54
    1. var BulletSpawn : Transform;
    2. var Gun : Transform;
    3. var Bullet : GameObject;
    4. var Zombie : GameObject;


    5. function Start () {
    6. }

    7. function Update () {
    8. if(Input.GetButtonDown("Fire1")){
    9. Shoot();
    10. }
    11. Network.Instantiate(Bullet, BulletSpawn.transform.position, BulletSpawn.transform.rotation, 0);
    12. }
    13. function Shoot(){
    14. var hit: RaycastHit;
    15. if (Physics.Raycast(transform.position, transform.forward, hit)){
    16. if (hit.transform.tag == "Zombie"){
    17. Debug.Log("Got a hit!");
    18. Zombie = hit.transform.gameObject;
    19. Zombie.SendMessage("TakeDamage()", "Sniper");
    20. }
    21. }
    22. }
    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.
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    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#
     
  3. The-Game-Master

    The-Game-Master

    Joined:
    Aug 6, 2014
    Posts:
    54
    How do I get a reference to the gameobject?
     
  4. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    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");
     
    The-Game-Master likes this.
  5. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    I'm pretty sure when using send message you do:

    SendMessage ("TakeDamage"...) you leave out the "()".
     
    The-Game-Master likes this.
  6. The-Game-Master

    The-Game-Master

    Joined:
    Aug 6, 2014
    Posts:
    54
    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!
     
  7. The-Game-Master

    The-Game-Master

    Joined:
    Aug 6, 2014
    Posts:
    54
    Also, when I lower the field of view (For a scope), it's too choppy. Is there a way I can fix that?