I’m having a hard time understanding what the SendMessage functions do exactly, and after doing a search around the forums for some info, it still seems pretty hazey.
Does it work like this?
Object1 has Script1 attached to it. Script1 contains a SendMessage function that calls Function2 when Object1 collides with another object.
Object2 has Script2 attached to it. Script2 contains Function2.
So when Object1 collides with Object2, the SendMessage function in Script1 should call Function2 from Script2 correct?
In more solid terms, when a bullet collides with an enemy or player object I want it to call a function to reduce the enemy or player’s HP. But I keep getting errors that the SendMessage has no receiver.
So I’m starting to think I don’t really understand the scope of the SendMessage function.
var obj : GameObject;
function Update() {
obj.SendMessage("FunctionName");
}
The above will, each frame, call the function FunctionName() on every script that defines it and which is attached to the GameObject obj.
function Update() {
SendMessage("FunctionName");
}
If you’re not calling SendMessage() on a particular GameObject, it applies to the same object the current script is attached to. So the above calls FunctionName() on every script (including the one that calls SendMessage) attached to the same object.
The error you get results from calling SendMessage() on an object with no attached script that defines the named function. There’s a parameter you can pass SendMessage() to hide the error, but first you might want to make sure the script you’re trying to call is attached where it should be.
Just a note to those of you reading this thread. I’m sure this is pretty noob stuff, but if you have arguments you are trying to pass in to the method, they’re considered options and you’ll need to use a different overload. It will look like this.
var obj : GameObject;
function Update() {
obj.SendMessage("FunctionName", arg1);
}
Again- probably stupid simple, but it might help spell it out even more clearly.