• 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
Question by xephin2004 · Oct 06, 2015 at 02:13 PM · movementaisendmessagenpcbroadcastmessage

SendMessage/BroadcastMessage has no receiver EnemyAI to NPCMovement

alt text here is the movement script:

 [RequireComponent(typeof(CharacterController))]
 public class NPCMovement : MonoBehaviour 
 {
     public enum State
     {
         _Idle,
         _Initialize,
         _Setup,
         _Action
     }
     public enum MoveVertical
     {
         Down = -1,
         None = 0,
         Up = 1
     }
     public MoveVertical _MoveVertical = 0;
     public enum MoveHorizontal
     {
         Left = -1,
         None = 0,
         Right = 1
     }
     public MoveHorizontal _MoveHorizontal = 0;
     //Enemy Movement
     public Transform _NPCPrefab;
     public CharacterController NPCMove;
     private Vector2 _Move = Vector2.zero;
     //Bools
     public bool Run = false;
     public bool Slow = false;
     //Move Speeds
     public float NPCSlowSpeed = 0.75f;
     public float NPCWalkSpeed = 1.5f;
     public float NPCRunSpeed = 3.0f;
 
     private State _State;
     //------------------------------------------
     void Awake () 
     {
         _NPCPrefab = transform;
         NPCMove = this.GetComponent<CharacterController>();
         _State = NPCMovement.State._Initialize;
     }
     //------------------------------------------
     IEnumerator Start () 
     {
         while(true)
         {
             switch(_State)
             {
             case State._Initialize:
                 Initialize();
                 break;
             case State._Setup:
                 Setup();
                 break;
             case State._Action:
                 Action();
                 break;
             default:
                 break;
             }
             yield return null;
         }
     }
     //------------------------------------------
     void Update ()
     {
 
     }
     void Action()
     {
         Moving();
     }
     void Initialize()
     {
         if(!GetComponent<CharacterController>())
         {
             return;
         }
         _State = NPCMovement.State._Setup;
     }
     void Setup()
     {
         _Move = Vector2.zero;
         Run = false;
         _State = NPCMovement.State._Action;
     }
     private void Moving()
     {
         _Move = new Vector2((int)_MoveHorizontal, (int)_MoveVertical);
         _Move = _NPCPrefab.TransformDirection(_Move).normalized;
         _Move *= NPCWalkSpeed;
         NPCMove.Move(_Move * Time.deltaTime);
     }
 }




here is the section I'm getting the error in my enemy ai script:

   if(distance >= StopRange)
             {
                 if(Direction.x > 0 && Direction.y > 0)
                 {
                     BroadcastMessage("MoveHorizontal", NPCMovement.MoveHorizontal.Right);
                     BroadcastMessage("MoveVertical", NPCMovement.MoveVertical.Up);
                 }
                 if (Direction.x < 0 && Direction.y < 0)
                 {
                     BroadcastMessage("MoveHorizontal", NPCMovement.MoveHorizontal.Left);
                     BroadcastMessage("MoveVertical", NPCMovement.MoveVertical.Down);
                 }
         }

ive used both send and broadcast but get simular errors I can move the enemy manully thru the inspector

screenshot-222.png (71.8 kB)
Comment

People who like this

0 Show 0
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

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by xephin2004 · Oct 06, 2015 at 06:39 PM

so instead of trying to send a message to my enums, I'm guess you cant, I made indivdual functions for moving up left right and down and used sendmessage to those in NPCMovement

        public void MoveUp()
         {
             _Move = new Vector2((int)MoveHorizontal.None, (int)MoveVertical.Up);
             _Move = _NPCPrefab.TransformVector(_Move).normalized;
             _Move *= NPCWalkSpeed;
             NPCMove.Move(_Move * Time.deltaTime);
         }

and in enemy AI

             Vector2 Direction = (Target.transform.position - Enemy.position);
             float distance = Vector2.Distance(Target.position, Enemy.position);
             if(distance >= StopRange)
             {
                 if(Direction.x > 0 && Direction.y > 0)
                 {
                     SendMessage("MoveRight", NPCMovement.MoveHorizontal.Right);
                     SendMessage("MoveUp", NPCMovement.MoveVertical.Up);
                 }

the enemy now follows the player around till the player is out of range just as intended

Comment

People who like this

0 Show 0 · 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

Answer by GlatiatorRX · Oct 06, 2015 at 05:11 PM

It looks like you do not have an actual function named "MoveVertical" or "MoveHorizontal" in your code. You have enums defined, but in order for BroadcastMessage, you need to input a function name in those quotations. Not just enum names.

If you are looking to simply update the vertical or horizontal values you have there, make a setter call kind of function. So like this:

 public void MoveNPCHorizontal(NPCMovement.MoveHorizontal inHorizontalMovement)
 {
     _MoveHorizontal = inHorizontalMovement;
 }
 
 public void MoveNPCVertical(NPCMovement.MoveVertical inVerticalMovement)
 {
     _MoveVertical = inVerticalMovement;
 }

And then call the function like this:

 if(Direction.x > 0 && Direction.y > 0)
 {
     BroadcastMessage("MoveNPCHorizontal", NPCMovement.MoveHorizontal.Right);
     BroadcastMessage("MoveNPCVertical", NPCMovement.MoveVertical.Up);
 }
 if (Direction.x < 0 && Direction.y < 0)
 {
     BroadcastMessage("MoveNPCHorizontal", NPCMovement.MoveHorizontal.Left);
     BroadcastMessage("MoveNPCVertical", NPCMovement.MoveVertical.Down);
 }

I have not used broadcast message much, so I cannot say if it will work, but if it does, great! If not, I'll be around more if there is still an issue. Good luck!

~ Matt

Comment
Theof

People who like this

1 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 Theof · Feb 16, 2019 at 12:35 PM 0
Share

Good tip!

Btw, your answer should take the praise. Not the issuer of the question that read your answer and then posted the corrected code, and never thanked!

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

How to forbid an NPC to move diagonally using Character Controller? 1 Answer

How to check if a character hasn't changed its position in the last x seconds? 1 Answer

How do I use Mecanim on an NPC? 2 Answers

How to make an enemy backpedal? 1 Answer

Help with AI follow range 0 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