- Home /
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
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.
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!
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
Follow this Question
Related Questions
Enable Child Component C# 1 Answer
enable/disable specific components 3 Answers
Can't disable/enable GameObject Component (C#) 1 Answer
enable/disable child objects? 2 Answers
disable/enable script js 2 Answers