- Home /
SendMessage has no receiver
Hey guys I'm trying to write a script where if the conditions are met it'll send a message to another script attached to an empty GameObject which will instantiate a GameObject (simple right?) Here's the script: function Start () {
}
static var targets : int = 0;
static var haveWon : boolean = false;
var winSound : AudioClip;
var cellPrefab : GameObject;
function Update () {
if(targets==3 &&haveWon == false){
targets=0;
audio.PlayOneShot(winSound);
haveWon = true;
cellPrefab.SendMessage("Appear");
}
}
@script RequireComponent(AudioSource)
And here's the empty GameObject's script: var cellHid : GameObject; function Start () { }
function Update () {
}
function Appear(){
Instantiate(cellHid, transform.position, transform.rotation);
}
The console is telling me that Appear has no receiver, and I'm not sure... well more like I have no idea what to do. So any help will be really appreciated. Thanks.
$$anonymous$$ost strange, this should work if your setup is as described. Needless to say, but have you checked that your Appear script is indeed attached to the gameObject cellPrefab?
Create a new scene.
add an empty gameObject, call it Sender, attach this script :
// Sender
var cellPrefab : GameObject;
function Update()
{
if ( Input.Get$$anonymous$$ouseButtonDown(0) ){
cellPrefab.Send$$anonymous$$essage( "Appear" );
Debug.Log( gameObject.name + " $$anonymous$$essage Sent ...." );
}
}
now add another empty gameObject, call it Receiver, attach this script :
// Receiver
function Appear()
{
Debug.Log( gameObject.name + " .... Received message :)" );
}
Now drop the 'Receiver' object into the 'Sender' inspector, then play the scene. When you left-click the mouse, you should see the Debug messages in the console. This is just a simple test to demonstrate Send$$anonymous$$essage.
it is attached to cellPrefab, but didn't quite understand what u meant by "received message" & "message sent" if I may ask
I edited my comment, hopefully it makes more sense. But your problem is strange, even if the script with function Appear() was attached to a child of the cellPrefab , Send$$anonymous$$essage would still work. Hence the error has no receiver is implying the gameObject cellPrefab cannot find function Appear() on any script on this object (parent or child). $$anonymous$$ake the test scene, check all is working as it should.
well ever heard of unity.3x.developpment essentials? I'm using that ebook to train me on using unity (only ebook that's more than 10 pages) and although I greatly appreciate the authors of the ebook they did a GREAT job, but it needs alot of fixes bcz 1/2 the scripts don't work, great book though, now I have done smthg tht worked I added a new GameObject to the 1st script, if conditions are met the object is instantiated, but it's lower than the floor the player is on. Any idea how to control the Y Axis in Instantiate?
Hang on ... so the test worked, and Send$$anonymous$$essage is proven working? You should submit an answer and accept it. The y_instantiate is a new question. But I can cover that here now =]
Instantiate( object, position : Vector3, rotation : Quaternion);
Instantiate( cellHid, transform.position, transform.rotation);
So the position of the instantiate is set by a Vector3. You are giving this Vector3 as transform.position. You can adjust this by adding it to another Vector3, eg
var adjustedPosition : Vector3 = transform.position + Vector3( 0, 0.5, 0 );
Instantiate( cellHid, adjustedPosition, transform.rotation);
this then adds 0.5 to the Y of the transform.position
IF that book is by Will Goldstone, then yes I have heard of it (though surprised half the code is wrong, unless you are using unity 4.0, then some things changed). In fact, I link his video series to all new users :
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/essential-skills/
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/beginner/
this is the YouTube link for the above as one playlist : http://www.youtube.com/watch?v=-oXYHNSmTxg&list=PL27B696FB515608D2&feature=plcp
Edit : the last video is Send$$anonymous$$essage : http://www.unity3dstudent.com/2011/02/beginner-b28-sendmessage-to-call-external-functions/