If I use sendmessage for a raycast for the hit object to deal damage to an enemy is there a way to send a return variable back to the player if the enemy was hit? I have the dealing damage part working but after damage is dealt I want to check if the enemy is dead and then send back a new message to give xp to the player for what level the enemy was.
Create a wrapper class that contains the damage to deal and the dealer of that damage and send that as the param in SendMessage.
Quick example? Tried looking it up but couldn’t find anything specific. By the way I’m using Javascript.
Off the top of my head - but should give you the idea.
public class DamagePacket
{
float Damage;
GameObject Sender;
public DamagePacket(float damage, GameObject sender)
{
this.Damage = damage;
this.Sender = sender;
}
}
public void DoDamage()
{
RaycastHit hit;
if (Physics.RayCast(...))
{
DamagePacket p = new DamagePacket(30f, gameObject);
hit.gameObject.SendMessage("TakeDamage", p);
}
}
public void TakeDamage(DamagePacket p)
{
this.Health -= p.Damage;
if (p.Sender != null)
p.SendMessage("GotHit", gameObject);
}
public void GotHit(GameObject target)
{
Debug.Log("I just hit " + target.Name + "!");
}
Looks like unity’s "java"script supports classes too…
http://wiki.unity3d.com/index.php?title=UnityScript_versus_JavaScript#JavaScript_is_class-free
… any one got a better reference on how they work?
Ah I see so you can setup a simple sender variable as a gameObject to be able to return the information. So would scripts 1,2,and 4 all be in one single script on the player firing? Then the 3rd script would be on the enemy? I haven’t worked much with classes before, I always end up getting errors. This helps me out a ton though, now I understand what you were saying.