• 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 wiivil-canevil · Apr 24, 2014 at 12:30 PM · child objectcollectible

Activate child only problem

Hello, I have a problem.

Its a simple collectible I made.

I have made it that when you collide with the object, it should disappear and play a spark effect. The spark effect is the child of the collectible. So the code simply hides the object when it collides, activates the child which is a particle system to play the particle effect and then destroys itself after 0.5 seconds.

So far everything works but I don't know how to activate the child as I don't want to activate just one part of the child, I want the whole child object to be activated.

The script is for the character, when the character enters the object, it will check the tag and do what the code tells it to do.

 void OnTriggerEnter(Collider other)
     {
         if(other.gameObject.tag=="bad")
         {
 
             //switch off renderer of current object to ensure that it cannot be seen
             other.gameObject.renderer.enabled = false; 
 
 //Need code to activate the child of the "other" object 
 
 
             Destroy(gameObject,0.5f);//destroy object after allotted time
             
             
         }
 
 
     }
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

Answer by Griffo · Apr 24, 2014 at 01:01 PM

try

 other.gameObject.transform.GetChild(0).SetActive(true);

if it's the first child, then

 Destroy(gameObject, 0.5);

but you won't see much particle effects in 0.5 seconds .. :)

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

Answer by HarshadK · Apr 24, 2014 at 01:00 PM

You can use Transform.Find() to get the child object of your "other" object and can set it to become active using GameObject.SetActive

So it would be like:

 *other.gameObject.Find("NameOfYourChildObject").transform.SetActive(true);*

The above line of code will not and does not work and as stated by @Wolfram, the correct code is:

 other.transform.Find("NameOfYourChildObject").gameObject.SetActive(true);
Comment
Add comment · Show 5 · 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 wiivil-canevil · Apr 25, 2014 at 11:57 AM 0
Share

They won't work for me. Says there is a type error between transform and set active. Does not make sense, I mean what you mentioned sounds like it should work...

avatar image Wolfram · Apr 25, 2014 at 12:02 PM 1
Share

SetActive is a method of the GameObject, not the Transform, so use: other.transform.Find("NameOfYourChildObject").gameObject.SetActive(true);

Also, there's no need ever to do a .gameObject.transform, you can always reference .transform directly.

avatar image Griffo · Apr 25, 2014 at 12:06 PM 1
Share

I didn't know that "you can always reference .transform directly" I'll have to go through my thousands of line of code and remove the gameObject reference .. :(

Like they say you learn something new every day.

avatar image wiivil-canevil · Apr 26, 2014 at 10:44 AM 0
Share

Thats awesome guys! Thanks Wolfram it worked! $$anonymous$$y life just got easier! Thanks again!

avatar image HarshadK · Apr 28, 2014 at 10:10 AM 0
Share

Thanks @Wolfram for correcting. I've updated the answer to reflect the correct code snippet.

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

22 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

Related Questions

How Can I Make Collectable Objects? 1 Answer

Creating object as a child in group by script 2 Answers

How to enable a specific gameobject child. 1 Answer

This does not make sense this.gameObject.transform.Find() why? 2 Answers

Changing Child objects' position 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