• 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 hero_kenshin · Mar 26, 2014 at 11:05 PM · javascriptinstantiate

Send message from an instantiated prefab to another

Hey again guys,

I am currently trying to get bullets working.

I have a system that spawns a space ship prefab, this spaceship prefab then moves around on its own and fires a bullet prefab, the bullet prefab use a script to detect collusion with an enemy spaceship prefab, I'm trying to get this bullet to call a function from whatever it hits.

To do this I'm using a sendmessage like this

function OnTriggerEnter( other : collider ) { if( other.tag == "Player" ) { var target : GameObject = other.gameObject; target.SendMessage("Take_Damage", 1 ); }

}

However this returns an error message saying there is no receiver and doesnt call the takedamage function.

Why is this? Its definatly hitting the enemy, the method i used before was actually setting a reference to the script of the enemy ship and calling the function by setting the collider object to the reference However, different ships use different scripts (though they will all have the same Take_Damage function) So i need something that can just call a function from whatever the script of the object it hits.

Does anyone have any idea of why this isn't working, or rather a different way to do this?

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 initTechsuport · Mar 26, 2014 at 11:29 PM

make sure the gameObject collider your hitting also contains the script your trying to talk to. if your not sure your taking to the right object try BroadcastMessage or SendMessageUpwards, but if there are multiple scripts that can be the receiver, it is only recived by the first one it finds.

Comment
Add comment · Show 2 · 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 hero_kenshin · Mar 27, 2014 at 12:35 AM 0
Share

The script of the enemy the bullet is hitting is defiantly attached to the collider being hit, i have tried Send$$anonymous$$essageUpwards but it yields the same result. There is only one script in the whole prefab so that shouldn't be a problem. I will try Broadcast$$anonymous$$essage, but whats the difference between Broadcast and send??

avatar image hero_kenshin · Mar 27, 2014 at 12:38 AM 0
Share

Woah, hold on. Send$$anonymous$$essageUpwards worked, it didn't before $$anonymous$$aybe it was because i changed the way things worked recently Thanks for your help initTechsupport!

avatar image
0

Answer by hero_kenshin · Mar 27, 2014 at 12:45 AM

The weird part is It only works on one of the ships now, even though they both have almost the same script (the only notable difference if one searches for a different tag)

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

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

20 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 avatar image

Related Questions

Referencing instantiated player in camera's Start() 2 Answers

Trying WaitForSeconds before Instantiating 2 Answers

How to Instantiate prefab as child? (Java) 1 Answer

Prevent instantiating on collider 1 Answer

Object instantiation Logic Javascript. 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