Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Frostbite23 · Jan 19, 2014 at 05:44 AM · receiver

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);
 
 }
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Frostbite23 · Jan 19, 2014 at 06:21 AM 0
Share

anybody..?

avatar image getyour411 · Jan 19, 2014 at 09:20 AM 0
Share

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?

avatar image Frostbite23 · Jan 19, 2014 at 05:12 PM 0
Share

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 ;(

avatar image Frostbite23 · Jan 19, 2014 at 05:28 PM 0
Share

anyone please?

avatar image Frostbite23 · Jan 19, 2014 at 05:54 PM 0
Share

hello? anybody????

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Frostbite23 · Jan 19, 2014 at 06:19 PM 0
Share

ah thank you, he finally shoots now! :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

GUIButton won't appear Scripting problem, help? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges