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 TheAlmightyPixel · Jan 01, 2015 at 12:56 PM · c#arraysendmessagereceiver

C# SendMessage to objects in array

So I've been pulling out my hairs one by one trying figure this one out. It's probably quite an obvious mistake, but I can't seem to find it. I'm trying to send a message to all enemies in the scene from the player when the player dies.

Here are the parts of the player script:

 public GameObject[] enemies;
 
 void Awake()
 {
    enemies = GameObject.FindGameObjectsWithTag("Enemy");
 }
 
 void Respawn()
 {
     for (int i=0; i<enemies.Length; i++) 
     {
        enemies[i].SendMessage("PlayerDied");
     }
 }

And the enemy script just has a function called "PlayerDied", which sets the enemy in a passive state. The problem is, I keep getting the error that says: 'SendMessage has no receiver!'

Any help would be appreciated!

Comment
Add comment
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

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

Answer by TheAlmightyPixel · Jan 01, 2015 at 02:36 PM

Okay, I got it to work. Basically my enemies have multiple child objects on them. They were all tagged with the tag 'Enemy'. So, instead of calling the PlayerDied() method on a 'Brain' child object of the enemy, the method was trying to be called on the enemy parent object, which is just a NavMesh Agent. All I had to do was to use a different tag on the brain object and search for objects with that tag...

I feel dumb for not realising that.

Comment
Add comment · 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
0

Answer by Landern · Jan 01, 2015 at 12:57 PM

try:

 enemies[i].SendMessage("PlayerDied", SendMessageOptions.DontRequireReceiver); 

That way if there is other components/scripts that dont' have that method on them, it won't throw an error RequireReceiver is the default when not specificied.

Comment
Add comment · Show 3 · 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 TheAlmightyPixel · Jan 01, 2015 at 01:56 PM 0
Share

I tried doing so, but now the function isn't ran at all. I tried using a Debug.Log just to see if it actually ran, but it's not doing anything.

avatar image Landern · Jan 01, 2015 at 02:00 PM 0
Share

Is the scope of the script directly attached to the array of GameObjects called enemies? Does enemies have a Length? What is the structure of one of the enemy gameobjects in the inspector? What does the other script look like that has PlayerDied method on it? Is it also c# or unityscript?

avatar image TheAlmightyPixel · Jan 01, 2015 at 02:25 PM 0
Share

The length of the array is the same as the amount of enemies in the scene. The enemies do actually have multiple child objects, with one of them being their 'brain' with the script. The enemy script is C#, and the PlayerDied method is a regular void method. I think it might be possible that the script isn't calling the function from the brain object, but on the enemy parent object itself, as I have them all tagged as 'Enemy'. I'll mess with the tags a bit and see how it goes.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GameObject not Scaling Correctly after Receiving Message 1 Answer

How to call a function from another script in C# from array? 1 Answer

SendMessage within a SendMessage Receiver 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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