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
0
Question by NinjaRubberBand · Nov 17, 2014 at 02:45 PM · parentchildparticle

Find a specific object even though there are many of them.

In my scene there are a lot of cactus prefabs, with a explosion particle as children. They all have the same name. I have a script attached to each cactus saying if i hit it, then a explosion will happen. This is my script:

 #pragma strict
 
 var emission = 100;
 
 function Start () {
     //Emission starts at 0, and when you hit the cactus it gets to 100.
     gameObject.particleSystem.emissionRate = 0;
 }
 
 function Update () {
 
     if(GameObject.Find("Cactus3").GetComponent(Explode).explosion == true) {
     //if i hit the cactus this will happen
    print("HI");
     gameObject.particleSystem.emissionRate = emission;
     }
 }

Since all of the cactuses are names cactus3 then all the explosion in the scene will detonate at same time even though i only hit one.

Can i say someting like if(GameObject.FindParent("Cactus3").GetComponent(Explode).explosion == true) {

for example if i have a child of a parent. This child has a script attached. In this script it says something like: if parent.explosion = true, then something.

I dont know how to explain this probably so if you have questions please ask.

Comment
Add comment · Show 1
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 robertbu · Nov 17, 2014 at 02:53 PM 0
Share

For future posts, please format your code. After pasting your code, select it and use the 101/010 button. I did it for you this time.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Immanuel-Scholz · Nov 17, 2014 at 03:13 PM

You can access your direct parent via transform.parent. So if the explosion prefab is a direct child of the cactus, just do

 if (transform.parent.GetComponent(Explode).explosion)
     particleSystem.emissionRate = emission;

Comment
Add comment · Show 1 · 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 Sir-Irk · Nov 17, 2014 at 03:27 PM 0
Share

I'd like to also point out for OP that accessing the parent this way will be much faster computationally then GameObject.Find(). Typically when you are thinking about using GameObject.Find() in an update you should try to rethink how you access that object as Find() is very slow. I think ideally you should cache Explode ahead of time to make the code both faster and cleaner. Just suggestions for OP and not required for solving the problem.

avatar image
1

Answer by Bunny83 · Nov 17, 2014 at 03:39 PM

Like GetComponentInChildren Unity now provides a method called GetComponentInParent. This will search up the hierarchy for that specific component. In your case I would suggest:

 var emission = 100;
 var explode : Explode;
 
 function Start ()
 {
     //Emission starts at 0, and when you hit the cactus it gets to 100.
     gameObject.particleSystem.emissionRate = 0;
     explode = GetComponentInParent(Explode);
 }
 
 function Update ()
 {
     if(explode.explosion)
     {
         print("HI");
         gameObject.particleSystem.emissionRate = emission;
     }
 }

However, if your cactus is a prefab, you simply should assign the "Explode" script to tha variable in the inspector. When instantiating a prefab such internal references will point to the internal object.

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

28 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

Related Questions

Make a simple tree 1 Answer

parent child relation btwn gammeobject and a ui canvas and ui text positioning problem 1 Answer

Creating new Transform from existing objects Transform to Instantiate object 1 Answer

How can I Instantiate a clone as a child of another object, and inherit the parent object's scale? 2 Answers

Need help dealing with unruly children! 2 Answers


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