• 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 SomeRandomGuy · Dec 05, 2011 at 04:25 PM · childcomponentdisableenable

Disabling/Enabling components in children of children?

 //Use on a large spherecollider with trigger enabled around character to disable particles on enemies when they are out of sight
 
 
 
 function Update()
 
 {
 
 
 
 }
 
 
 
 //trace all collisions and assign them to the col variable
 
 function OnTriggerEnter(col : Collider)
 
 {
 
 
 
 //if the tag of col is not "Enemy" then find all objects that do have the "Enemy" tag and turn off the particlerenderer on them
 
     if(col.gameObject.tag !="Enemy")
 
     {
 
         PartEmit = GameObject.FindWithTag("Enemy");
 
         PartEmit.GetComponentsInChildren(ParticleRenderer).enabled = false;
 
     }
 
 
 
 //if col does have the "Enemy" tag, then turn on its particlerenderer
 
     if(col.gameObject.tag =="Enemy")
 
     {
 
         col.GetComponentsInChildren(ParticleRenderer).enabled = true;
 
     }
 
 
 
 }


Ok, so this is a script that should disable particles when they are too far from the player. This code does run, however it gives me the following error:

 MissingFieldException: Field 'UnityEngine.Component[].enabled' not found.
 Boo.Lang.Runtime.DynamicDispatching.PropertyDispatcherFactory.FindExtension (IEnumerable`1 candidates)
 Boo.Lang.Runtime.DynamicDispatching.PropertyDispatcherFactory.Create (SetOrGet gos)
 Boo.Lang.Runtime.DynamicDispatching.PropertyDispatcherFactory.CreateSetter ()
 Boo.Lang.Runtime.RuntimeServices.DoCreatePropSetDispatcher (System.Object target, System.Type type, System.String name, System.Object value)
 Boo.Lang.Runtime.RuntimeServices.CreatePropSetDispatcher (System.Object target, System.String name, System.Object value)
 Boo.Lang.Runtime.RuntimeServices+<SetProperty>c__AnonStorey16.<>m__C ()
 Boo.Lang.Runtime.DynamicDispatching.DispatcherCache.Get (Boo.Lang.Runtime.DynamicDispatching.DispatcherKey key, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
 Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
 Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
 Boo.Lang.Runtime.RuntimeServices.SetProperty (System.Object target, System.String name, System.Object value)
 ParticleDisabler.OnTriggerEnter (UnityEngine.Collider col) (at Assets/Standard Assets/Scripts/General Scripts/ParticleDisabler.js:16)


So I looked it up and it seems that for some reason gameObject.active = true/false would work better in some cases, so I tried that and it just gave me a different error:

 NullReferenceException: Object reference not set to an instance of an object
 Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
 Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
 Boo.Lang.Runtime.RuntimeServices.SetProperty (System.Object target, System.String name, System.Object value)
 ParticleDisabler.OnTriggerEnter (UnityEngine.Collider col) (at Assets/Standard Assets/Scripts/General Scripts/ParticleDisabler.js:16)


Right now I have a felling that it has something to do with where my particles are, as my enemies have the standard assets "Fire1" particle system on them and the spawners for these enemies have the "Enemy" tag. The spawners are parent objects of the actual enemies, so I thought this should just work, but it seems that somehow it can't find the particle systems?

Any idea what is going on? Could it perhaps be that GetComponentsInChildren doesn't work on child object of the parents child objects?

Anyways, Thanks in advance! :D

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 Eric5h5 · Dec 05, 2011 at 08:36 PM

GetComponentsInChildren returns an array of Component[]. As the error says, there's no such thing as .enabled for an array. You can iterate through the array and use .enabled on the individual components.

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 SomeRandomGuy · Dec 06, 2011 at 07:30 AM 0
Share

Ah ofcourse, seems logical... I'll have to take a look at how I can go through that array then. $$anonymous$$ight need some help with this, as I'm pretty sure I haven't done that before in unity...o.o But for now I'll just try to look it up and see how it works, So thanks!

avatar image SomeRandomGuy · Dec 07, 2011 at 05:03 PM 0
Share

Ok, I suppose that would work, however I can't seem to figure out how to access the array made by GetComponentsInChildren. How would I do this? normally an array would be accessed by a variable name, but I don't really have one here, how would I still be able to access this?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Unable to enable script 0 Answers

How to disable/enable SSAOEffect or other specific Components? 1 Answer

Enable Child Component C# 1 Answer

enable/disable specific components 3 Answers

Can't disable/enable GameObject Component (C#) 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