Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 jvt619 · Feb 17, 2014 at 10:25 AM · getcomponentparentchildeventdelegates

GetComponent() vs Delegate/Event

For example I have 5 doors with the same Tween script to open and close, I also have 5 "Triggers" that controls a certain door and has a children "ChildTrigger" that knows that it has been switch(like in Portal 2) and then tells the parent trigger that it's been activated and the trigger parent will activate or control the door depends on how many children has been activated.

     ParentScriptTrigger has variables like:
     private int ChildTriggerCounter = 0; //When starting the game, this will increment on how many gameobject children does he has to be activated
     public int ActivatedChildTrigger = 0; //Counter that will activate if it is equal to ChildTriggerCounter
     private bool isActivated = false;
     void Start()
     {
        foreach(Transform child in transform)
        {
           ChildTriggerCounter++;
        }
     }
     
     void Update()
     {
        if(ChildTriggerCounter == ActivatedChildTrigger && !isActivated )
        {
           //Activate the door (Open or Move)
           isActivated = true;
        }
        else if(ChildTriggerCounter < ActivatedChildTrigger && isActivated )
        {
           //Deactivate the door
           isActivated = false;
        }
     }

and the Childtrigger script has:

 private GameObject keyGO;
 private ParentScriptTrigger parentScript;
 void Start()
 {
     parentScript = transform.parent.GetComponent<ParentScriptTrigger >();
 }
 
 void OnTriggerEnter(Collider other)
 {
     if(other.tag == "Key" && !keyGO)
     {
        parentScript.ActivatedChildTrigger++;
        keyGO = other.gameObject;
     }
 }
 
 void OnTriggerExit(Collider other)
 {
     if(other.tag == "Key" && keyGO)
     {
        parentScript.ActivatedChildTrigger--;
        keyGO = null;
     }
 }

Should I use a transform.parent.GetComponent() then increment a value of the parent's script or an event/delegate to the "ChildTrigger"?

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 Nicolaj Schweitz · Feb 17, 2014 at 11:03 AM

In this case, I I'd keep it simple and make a public variable and simply assign it in the editor and get on with it. At some point, when the size of the project calls for it, I'd reevaluate the implementation.

The risk when using the in-editor-assigned variable, is that the project might be garbled at some point and then you'll have to remember what was assigned to the variable on the monobehavior in the editor. To address that risk, you should go with either the GetComponent() or the delegate method.

Alternatively, you could use SendMessage, but that opens another can of worms, namely the performance can, which in this case, does not seem to be the issue. (Check out this answer for a note on performance between SendMessage, Delegates, and method calls)

Then, to answer your question: GetComponent vs Delegate?

In this particular case: Pick what you find easiest to maintain.

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

19 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

Related Questions

Make a simple tree 1 Answer

How can i make GetComponent(SCRIPT) apply to ONLY ONE object that has the script? 1 Answer

Connecting Multiple Rigidbodies with Collision 0 Answers

Talking To Other Scripts 1 Answer

Firing OnTriggerEnter in the parent from a child without a script 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