• 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 /
  • Help Room /
avatar image
0
Question by Gentatsu · Feb 07, 2017 at 01:24 PM · childevent handlers

Add an event listener when GameObject becomes a child of another

I'd like to have a listener on my gameobject for when it becomes a child of another gameobject, so I can perform functions based on what it's added to.

Any good of way of doing it, or is this a silly thing to do anyway?

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 nasir_41 · Feb 07, 2017 at 01:33 PM

 bool isEvenFired=false;
 public delegate void IsChild(bool status);
 public event IsChild ChildEvent;
 
 void Update(){
 if(!isEventFired){
 isEventFired=true;
 if(ChildEvent!=null){
 ChildEvent(transform.parent!=null);
 }
 }
 }
 

And in other scripts you just need to assign a method to this delegate and event will be fired.. Hope this will give you a quick idea about it.

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 Gentatsu · Feb 07, 2017 at 02:38 PM 0
Share

Is there a way to do it without it being in an Update function? That's one of the things I was trying to avoid! Thanks!

avatar image nasir_41 · Feb 08, 2017 at 05:08 AM 0
Share

Yes use this $$anonymous$$ethod void OnTransformParentChanged(){ }

See the docs https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnTransformParentChanged.html

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

Child skews when parenting 1 Answer

Can someone explain the whole "child" and prefab thing? 1 Answer

Unity 5 - How to addtorque to rigidbody without overriding axis constraints? 0 Answers

How to Insantiate an object as a child???!!! 0 Answers

Making an object spawned from a list became a child of the player? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges