Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

MissingMethodException: Transform.BroadcastMessage

Discussion in 'Scripting' started by wertymk, Jul 31, 2010.

  1. wertymk

    wertymk

    Joined:
    Dec 13, 2009
    Posts:
    104
    I'm trying to do a BroadcastMessage on the root of the transform I'm getting from a RaycastHit, but I get the following error: "MissingMethodException: Method not found. UnityEngine.Transform.BroadcastMessage."

    What is going on? The same thing works elsewhere in my scripts with Collisions, but doesn't work with RaycastHit.

    For example this works:
    Code (csharp):
    1.  
    2. OnCollisionEnter(col:Collision){
    3.    col.transform.root.BroadcastMessage("DoSomething");
    4. }
    5.  
    But this doesn't:
    Code (csharp):
    1.  
    2. var hit:RaycastHit
    3. Physics.Raycast(pos, dir, hit, dist);
    4. hit.transform.root.BroadcastMessage("DoSomething");
    5.  
     
  2. menneske

    menneske

    Joined:
    Jan 23, 2009
    Posts:
    44
    If the raycast doesn't hit anything, I'd expect you'd get errors with that code. Try this:

    Code (csharp):
    1.  
    2. var hit:RaycastHit
    3. if( Physics.Raycast(pos, dir, hit, dist) )
    4. {
    5.   hit.transform.root.BroadcastMessage("DoSomething");
    6. }
    7.  
     
  3. wertymk

    wertymk

    Joined:
    Dec 13, 2009
    Posts:
    104
    The example was just a simplification, I'm actually doing Physics.RaycastAll() and looping through the results. If nothing is hit the broadcasts isn't attempted.
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    BroadcastMessage has an optional third parameter that specifies what to do when an object doesn't have the function called by the broadcast. By default, it requires the receiving script to have the right function and throws an error if it doesn't. However, if you change the parameter to SendMessageOptions.DontRequireReceiver, the message can be sent to an object that can't respond without any error. This is especially important with BroadcastMessage, since it is easy to send messages to objects unintentionally.
     
  5. menneske

    menneske

    Joined:
    Jan 23, 2009
    Posts:
    44
    I though having no receiver would give the
    Code (csharp):
    1. BroadcastMessage methodName has no receiver!
    error message? Isn't unity supposed to catch the MissingMethodException?
     
  6. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Erm... yes, you're right about that - my mistake!

    @Wertymk: can you post the code that doesn't work exactly as you have it in the script?