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
-1
Question by Daniel 1 · Jul 14, 2010 at 12:30 AM · parentchildbroadcastmessagefindobjectoftype

Calling a function of another object's child?

I got an individual script attached to two different weapon-objects: weapon_1 and weapon_2. Both scripts have a different version of the same function called Fire(). Both are children of the parent-object: weapons.

Here's the problem: The fire-button is placed outside this hierarchy, but I want to be able to reach whatever weapon's script is active at a given time (either weapon_1 or weapon_2).

Since the name of the script varies depending on which weapon is active, I can't use the method of searching for the script's name:

var myWeapons : weapons = FindObjectOfType(weapons);
myWeapons.Fire();

Another method I tried was using the "BroadcastMessage", but since that only works for the object itself or its children, and in my case I want to reach it from outside the hierarchy, I tried this approach:

var weapons : GameObject;
...
weapons = GameObject.Find("weapons");
weapons.BroadcastMessage("Fire");

Unfortunately this won't work. It seems when using the "BroadcastMessage" on another object than the one making the call, the function skips checking the children of the targeted object.

So, is there another way of targeting the child of another object dynamically like in the second proposed but invalid method above?

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
0

Answer by Daniel 1 · Jul 14, 2010 at 01:01 AM

It appears the error was due to my own stupidity of having renamed the "weapons"-object without editing the second line in the second sample. Having done so the "BroadcastMessage" does indeed look for functions in the targeted object's children.

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Make a simple tree 1 Answer

Can I limit a .Find to the Parents and Child and not the Scene? 3 Answers

Make object A parent of object b 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to destroy a parent object when child object is collided 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