unity, c#, and sendmessage

I’m relatively new to Unity and c# and trying to understand how to use SendMessage.

If I instantiate a prefab that has a c# script attached that has a method called SetParam().

After I instantiate a prefab from another script what is the syntax for me to do a sendmessage from the script where I just instantiated the prefab (now a GameObject) and only call that GameObjects SetParam() method?

The code below is wrong, of course, but essentially what I want to do:

public class wcw_mainScript : MonoBehaviour

{

void Update()

{

Instantiate (nextBadGuy, new Vector3 (xpos, ypos, zpos), Quaternion.identity);

nextBadGuy.AttachedScript.SendMessage ("SetParam", 3);  // << What is the correct syntax here?

}

}

Thanks for any help!

As stated by @lttldude9 (sorry if I misspelled) you can use GetComponent for the reasons that it goes faster than SendMessage.
On the other hand, SendMessage is easier and more convenient. If you use it once in a while it won’t make a big difference.

The big difference would come if you use it a lot then I would recommend GetComponent and a cache. Cache means that you fetch the component in the Awake or Start function so that it is kept in the cache. (I would guess it means the cache memory which is a memory zone physically closer to the cpu then faster to access-to be confirmed-)

The point being that unity does not need to find the component each time you need it, it is fetched and assign to a variable for the rest of the program.

here are unity script reference
http://unity3d.com/support/documentation/ScriptReference/GameObject.SendMessage.html
http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html

here a nice tutorial on SendMessage
http://www.unity3dstudent.com/2011/02/beginner-b28-sendmessage-to-call-external-functions/
And on GetComponent
http://www.unity3dstudent.com/2010/07/beginner-b17-tweaking-components-via-script/
Do your choice now

Disclaimer: I’m far from a c# expert, so may need to adjust it a bit.

First thing, SendMessage should be after a GameObject or Component, not the attachedScript. If you are able to use GetComponent to retrieve the script, then you don’t need to use a send message. It would look something like this:

ScriptName other;
other = nextBadGuy.GetComponent("ScriptName") as ScriptName;
other.SetParam(3);

If I understand correctly, a script is instantiating an object, then you want to run a certain function on that new object right? So the above line should work. And to know which object to send it to, save the gameObject as you instantiate it, something like this:

GameObject instNextBadGuy = Instantiate (nextBadGuy, new Vector3 (xpos, ypos, zpos), Quaternion.identity);

Then call the function:

ScriptName other;
other = nextBadGuy.GetComponent("ScriptName") as ScriptName;
other.SetParam(3);

OR

instNextBadGuy.SendMessage("SetParam", 3);

Hope this helps. Please ask if you still need help.