- Home /
Unity - AIfire Has No Reciver!
Hi Guys, Im developing my 2d game and I'm currently working on my AI, however when i send a message to tell that my AI has to fire its weapon its not firing and it spits out an error.
Heres my AI code -
if(Vector2.Distance(transform.position, Player.position) >= ShootDist){
rigidbody2D.velocity.x = 0;
timer += Time.deltaTime;
if(timer > 0.1){
timer -= 0.1;
SendMessage("AIfire", SendMessageOptions.RequireReceiver);
}
}
and heres the function in my AI weapon script
function AIfire () {
yield WaitForSeconds(FireRate);
Instantiate(Muzzle, EffectSpawn.position, EffectSpawn.rotation);
Instantiate(Tracer, TracerSpawn.position, TracerSpawn.rotation);
audio.PlayOneShot(ShootSound);
var trans : Vector2 = new Vector2(transform.position.x, transform.position.y);
// var hit : RaycastHit2D;
var fwd3 = transform.TransformDirection(Vector2.right);
var fwd = Vector2(fwd3.x, fwd3.y);
Debug.DrawRay(trans,fwd);
var hit : RaycastHit2D = Physics2D.Raycast(trans,fwd,range);
if(hit != null){
if(hit.collider.tag == "Concrete"){
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
Instantiate(ConcreteImpact,hit.point,Quaternion.FromToRotation(Vector2.up,hit.normal));
}
//If shoot an object taged as Metal then spawn a Metal Hole And a Metal Impact at the exact pos when it was hit
if(hit.collider.tag == "Metal"){
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
Instantiate(MetalImpact,hit.point,Quaternion.FromToRotation(Vector2.up,hit.normal));
}
//If shoot an object taged as Glass then spawn a Glass Hole And a Glass Impact at the exact pos when it was hit
if(hit.collider.tag == "Glass"){
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
Instantiate(GlassImpact,hit.point,Quaternion.FromToRotation(Vector2.up,hit.normal));
}
//If shoot an object taged as Blood then spawn a Blood Hole And a Blood Impact at the exact pos when it was hit
if(hit.collider.tag == "Blood"){
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
hit.collider.SendMessageUpwards("Damage", damage, SendMessageOptions.DontRequireReceiver);
Instantiate(BloodImpact,hit.point,Quaternion.FromToRotation(Vector2.up,hit.normal));
}
//If shoot an object taged as Untagged then spawn a Untagged Hole And a Untagged Impact at the exact pos when it was hit
if(hit.collider.tag == "Untagged"){
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
Instantiate(UntaggedImpact,hit.point,Quaternion.FromToRotation(Vector2.up,hit.normal));
}
}
//Apply Damage When an object is hit (only works when the object has a script with a function named ApplyDamage, aka a damage script)
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
I don't use the Send$$anonymous$$essages but from the doc it looks like they are supposed to be on the same gameobject; are both of these on the same gameobject?
no, because in if the weapon script is in the same place as my AI script the ray cast spawn is in the middle of the enemy, if i make him shoot he will be shooting him self so i have to make it so the script is on the weaponSprite and when the AI reaches the ShootDistance I want the AI to send a message TO the weapon script, I've tried broadcast message but i did not get any luck at all ;(
Answer by Anxo · Jan 19, 2014 at 06:01 PM
If the Object A is using "SendMessage" The AIfire function must be on OBject A. If you want to send a message to another Object, ObjectB then you need to call it like this.
objectB.SendMessage("AIfire");
If it is on a child of object B you have to call it like this.
objectB.BroadcastMessage("message");
to call the function.
Your answer
Follow this Question
Related Questions
Sir i have parsing error please help 0 Answers
SendMessage within a SendMessage Receiver 0 Answers
How can i display a pdf file in a unity scene ? 2 Answers
Raycast hitting the incorrect layer 2 Answers