Hi, first time using the Unity’s SendMessage function. I’ve tried many tutorials and sources but they don’t seem to work for me.
This is the code so far:
function DamageThePlayer (damage : float)
{
person.health -= damage;
}
“person” is the class I made in the same script that is WITH this one, as well as the health. Damage variable is announced when… well pretty straight forward…
var dmgOfTheEnemy = 5;
function OnCollisionEnter(collision : Collision)
{
print("Beginning to touch..."); //I know when the enemy has hitted the player...
collision.collider.SendMessage( "DamageThePlayer", dmgOfTheEnemy); //I'm I doing this right??!!
print("I DAMAGED THE PLAYER! WOOHOO!"); //Know when the enemy has successfully damaged the player
}
Above is the enemy’s script.
This is the Hierachy:
Player:
-Player
–Person
—Character <–The PLAYER script (which is the DamageThePlayer script) is in the inspector of gameObject Character
—PlayerCamera
Enemy:
-Enemy <–The ENEMY script is in here, only 1 gameobject for whole.
In your hierarchy, what has the acutal collider on it?
I would suggest putting all the player scripts on the base object, not way up in the hierarchy of it. Then in your script for the enemy, use the SendMessageUpwards Method if the SendMessage method doesn’t seem to be doing the trick.
Truthfully, the base object of a character should have the collider, but in the case of when you want to make colliders for specific areas like arms, legs and chest and so forth, you wouldn’t do that. (hence, SendMessageUpwards would send the message to the parents of the colliding object)
And do give people time to reply… I do realize that 15 minutes is a long time to wait.
Heh, yea SendMessageUpwards was the first thing I tried, forgot to mention that. Didn’t work as well. Actually, this is a middle of the project of a MMORPG, and I already made the simple AI following the player and a network test as well.
Player Gameobject is suppose to be the object the ENEMY follows, which is suppose to be any enemy, whenever which name it has or class it is assigned to, and is the main object for the GUI’s.
Person Gameobject is suppose to be the username of the player, as in the DamageThePlayer script, it also has that variable in the class.
Character Gameobject is the player’s mesh design, which is the MAIN collider, and where all the animations begin.
PlayerCamera is pretty straight forward.
Actually, most of my posts were about 2 days before they are actually replied, so…
Thanks for trying to help, but it also doesn’t work. I should had mentioned that it does not print its message onto the Debug nor the console, so it must be either something blocking it from accessing this path. I will try to work around this.
EDIT:
I did try SendMessageOptions.DontRequireReceiver, as I, usually check the resources and the official documentations first before moving on here, as that line was explained by HiggyB.
Yo!
Did you see what I was trying to do there? I wasn’t trying to get it to work, I was trying to see which object sends the message and which objects receives the message. This wasn’t meant to make your code work… it was a debug
So now the debug starts: First of all, the OnColissionEnter function, did THAT print a message? If not, read the requirements in the doc carefully. Needs a rigidbody, doesn’t need a rigidbody etc. When I got started this caused me endless grief. Hell, in one instance, to get my character to behave the way I wanted it to I had to attach 3 separate colliders to it’s hierarchy before it all worked the way it was supposed to. I.e. collider on same object that had the mesh renderer but another piece of coding required a collider at a higher level etc etc etc. OnCollisionEnter is quite a tempermental function to get working, in my opinion
If you are not getting a message here, then read the requirements in the doc and modify your objects accordingly until you see a message here. If you DO see a message here then you need to check your logs for error messages along the lines of “GameObject does not have a receiver”. If you are getting this then it means the second part of the script is in the wrong place in your target hierarchy. If you are NOT getting this message then it means that SOME cmponent in your target object has a function with the same name and THAT one gets called. Remember also the difference between SendMessage and BroadcastMessage. SendMessage sends the message down the hierarchy UNTIL it reaches something that accepts the message. BroadcaseMessage sends the message to all objects in the hierarchy and let all components that accept the message accept the message. Thus, if you have another component between the target object and the component you want to execute and THAT component has a function with the same name, sendmessage will stop right there.
So to recap:
Does OnColission trigger a message? If not, is the collider marked as trigger? If so, uncheck it. If still no message, the error is with your colliders. Check the requirements for OnCollision in the docs.
If the message fires, check the name of the object that is receiving the message. Is this the right object? If so, check for an error message along the lines of “No receiver”.
If you get that error, it means your code is not reached on the target object. Try doing the SendMessageUpwards. If you still get the error then you have a spelling mistake in your function name or your component is disabled.
If you do NOT get the “No receiver” message then it means that something on the target is receiving the message. Try replacing SendMessage with Broadcast. If your code still doesn’t work, you have a spelling mistake in your function name or the component is turned off. Look through each component in each object in the hierarchy and see if any other component has a function with the same name