• 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 MorbidDesign · Jun 08, 2017 at 03:29 PM · prefabsdestroy objectsendmessage

SendMessage has no receiver

Hello.

I'm having some troubles with the SendMessage method. This is the situation:

My player tries to shoot a bouncing ball, and when the harpoon hits the ball, it is supposed to disappear (yep, it is some kind of Pang clone...). Now, the code.

 public class ShootController : MonoBehaviour {
 
     // Player shoots
     public Transform shootPosition;
     public GameObject shoot;
     private bool chainFired = false;
     private GameObject chainSprite;
 
     void Update () {
     
         if (chainFired) {
 
             chainSprite.transform.localScale += new Vector3 (0, 0.4f, 0);
             chainSprite.transform.position += new Vector3 (0, 0.2f, 0);
         }
     }
 
     void Shoot () {
 
         if (chainFired) {
 
             Destroyer (chainSprite);
         }
         chainSprite = Instantiate (shoot, shootPosition.position, shootPosition.rotation) as GameObject;
         chainSprite.name = shoot.name;
         chainFired = true;
     }
 
     void Destroyer (GameObject gameObject) {
 
         if (gameObject.tag == "Shoot") {
 
             chainFired = false;
         }
 
         Destroy (gameObject);
     }
 }

Inside the BalloonController:

 void OnTriggerEnter2D (Collider2D other) {
 
         if (other.gameObject.tag == "Shoot") {
 
             shootController.SendMessage ("Destroyer", other.gameObject);
             Destroy (this.gameObject);
         }
     }

And here there a couple of screenshots from the inspector:

alt text

alt text

What I'm doing wrong? Maybe that Shoot prefab shouldn't be assigned to the Shoot param in ShootController script? I've read around here that prefabs don't work with SendMessage, but I'm still missing an alternative.

Thanks for your help!

shootmanager.png (66.8 kB)
ballooncontroller.png (113.5 kB)
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

1 Reply

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

Answer by GameVortex · Jun 08, 2017 at 07:00 PM

The "Destroyer" function takes a GameObject as a parameter and you are providing it with the object "other" which is a Collider2D. This makes the system look for a Destroyer function which takes a Collider2D as a parameter, which is a function that does not exist causing the No Reciever error. You should provide it with the GameObject you are using to compare the tag: "other.gameObject".

That being said I am wondering why you need to use SendMessage? You do seem to have a direct reference to the ShootController. You could easily make the Destroyer function public and call it directly:

shootController.Destroyer(other.gameObject);

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 MorbidDesign · Jun 11, 2017 at 04:38 PM 0
Share

Sorry, @GameVortex, but after some tweaking I left the wrong code. What I really have written down was other.gameObject, as you suggest. But it doesn't work.

What I found out is that if I put all the objects that I want to instantiate in some location out of camera, the reference to the shootController script is never lost, so I can use Send$$anonymous$$essage without problem.

The main reason why I need to do this is to change the value of a bool variable inside shootController, in case that the object to destroy is a "shoot".

avatar image GameVortex MorbidDesign · Jun 11, 2017 at 05:59 PM 0
Share

Then I am not quite sure what your exact issue is, as you are saying you can use the Send$$anonymous$$essage without problem? Are you getting any error messages? If so then please copy the whole message including the line number. Perhaps describing the problem with a few more details.

Also I did not question why you needed to call the function Destroyer, I was wondering why you needed to use Send$$anonymous$$essage to do so ins$$anonymous$$d of making the Destroyer function public and calling it directly as in my example above.

avatar image MorbidDesign · Jul 09, 2017 at 05:18 PM 0
Share

Thanks @GameVortex, I was a little bit obfuscated. You're right, Send$$anonymous$$essage was not neccesary and by making my function public the problem is over.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Wrong coordinates of setting position of the object 1 Answer

Make a game object in a scene into a prefab with script 1 Answer

How to shoot prefab, and then on colliding destroy a gameobject? 2 Answers

GameObject.FindGameObjectsWithTag() to find Prefabs which are not in the scene 1 Answer

How to programmatically break prefab instance upon adding to hierachy 1 Answer


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