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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by ncr · Mar 22, 2013 at 10:17 AM · eventorganization

How to delegate events from one GameObject to another?

I have a CharacterController and a script that implements OnTriggerStay. When I attach the script to the CharacterController everything works as expected.

However I would like to keep the script somewhere else and still be able to react on events from CharacterController. In my particular example, I have an Inventory prefab which has the script attached and I would like just to drag the inventory prefab and add it to my player object and have everything wired automatically. Currently I do as follows:

 private var target : GameObject;
 target = transform.root.gameObject.FindWithTag("Player").GetComponentInChildren(CharacterController).gameObject;
 
 // Avoid The Infinite Loop
 if (!target.GetComponent.<InventoryController>()) {
   target.AddComponent(InventoryController);
 }


and the structure of GameObjects is like this:

 Player
   First Person Controller
   Inventory

Is there a better way to do this? Or maybe I shouldn't do it and just attach the InventoryController to the CharacterController?

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
Best Answer

Answer by Owen-Reynolds · Mar 22, 2013 at 03:36 PM

It looks a little like your asking if you can have a gameObject with a charController and 2 scripts (inventory, movement.) Sure you can. Or, answer#2:

The object with the component is always going to be the one to detect it. So, you can't put OnTriggerStay anywhere except in a script on the gameObject with the charController component (well, in the thing you're touching, but not somewhere else listening to you.)

But, you can no problem have On** call out to anywhere (the cool kids way, where you make a link 1st):

 InventoryController IC; // link to main inventory script
 // establish link to inventory on some empty:
 Start() { IC = GO.Find("InventoryGO").GetComponent<InventoryController>(); };

 OnTrigger*(...) {  IC.doSomeCheck(transform, thingIHit); }
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 ncr · Mar 25, 2013 at 12:42 PM 0
Share

After a few days more with Unity, I realized that I don't need to delegate events at all. I simply should add a collider to the Inventory game object so it emits events directly to my script. I think this is the correct way of adding functionality to game objects: attach a child game object and add some scripts to it. This way I can also make a prefab from the Inventory along with scripts to use later on other game objects.

avatar image Owen-Reynolds · Mar 25, 2013 at 02:09 PM 0
Share

Often the inventory script would reuse previously setup player colliders. I believe 2 OnCollisions (on two scripts) will trigger (but I think colliders may not count co$$anonymous$$g "down" from a parent.)

But let's say you don't pick up objects if you're hurled through them. So you might want the player to handle all collisions/clicks and call "Inven.AddItem("fish");" as needed. It still might make sense to add the Inventory script/object to the player -- since it belongs to you.

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

11 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

Related Questions

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

Singleton that can react to game events 2 Answers

Open a link without popup-blocker 3 Answers

Example of setting up a button OnClick event via scripting? 2 Answers

Initiating several events on collision 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