- Home /
'UnityEngine.Component.active' is obsolete.
Hey guys,
I have a gameObject composed of a parent and many child gameObjects. I've been trying to toggle some of these child transform with:
gameObject.transform.Find("MyChild").active = false;
It works, except the compiler throws a warning about 'UnityEngine.Component.active' being obsolete.
I'd really like to solve this issue but I've tried casting to Transform, GameObject, Component using both the 'active' and 'enabled' property but all attempts cause either an error or a warning... I'd appreciate any hint about what I'm doing wrong.
EDITED to correct the poor choice of words.
With retrospect, I really have no idea why I kept trying to work on the returned Transform component and not uproot to its GameObject. For some reason it seems like this was just reaching back to the parent GameObject, but now I see why I was being thrown off and its only logical... totally my mistake, thanks alot guys!
I have like 20 obsolete warnings messages and yet everything works fine on $$anonymous$$e. Just try to live with the warnings?
Answer by Tseng · Oct 17, 2011 at 02:49 AM
Deprecated warning means: It will be removed in the future and won't work. So if you want a solid code which will also work in future versions, use gameObject.active instead.
That would be: gameObject.transform.Find("MyTransform").**gameObject.**active = false;
Answer by syclamoth · Oct 17, 2011 at 02:51 AM
Why not just... you know... do what it tells you to?
Every time you have something like
someTransform.active = someBool;
replace it with
someTransform.enabled = someBool;
That'll get rid of the warnings, and make your code more forward-compatible.
Also, I wasn't aware that having multiple transforms on a single gameObject was an even slightly good idea. Are you sure that's what you want to do?
AMENDMENT Sorry, you were right, you can't do that. Which means that what you are trying to do (or more accurately, the way you are trying to implement it) is impossible. What are you trying to achieve? There has to be a better way than trying to attach multiple transforms to a gameObject. In fact, disabling transforms doesn't even make sense- if a transform is disabled, how does the engine know where in the game world the object is supposed to be? Having several transforms which the object cycles between seems... awkward. Are you sure you shouldn't be storing a list of transforms on other objects which the main gameObject cycles through, setting up parenting and position/rotation?
Because... you know... $$anonymous$$issingFieldException: Field 'UnityEngine.Transform.enabled' not found.
Yea, that's because Transform directly derives from Component
, while Scripts for example derive from $$anonymous$$onoBehaviour
which derives from Behaviour
which as .enable implemented, or Renderer
which implements enable
too and derives from Component
Answer by CHPedersen · Oct 17, 2011 at 06:36 AM
The reason it's a good idea to use non-depricated code has already been explained, so I won't go into that. :)
The correct property to enable/disable depends on what you're trying to do in your program. Setting active = true/false isn't the same as setting a component's enabled = true/false, and in addition to this, Component.enabled does something different depending on which kind of component you're setting it on. Setting enabled = false on a Script, for example, prevents the runtime system from calling any of the methods that update it (like Update, OnGUI, etc.), while setting enabled = false on a Renderer will just cause it to disappear - it'll still exist in all other respects, as in, you can collide with it or move it around, it just won't get rendered.
Setting GameObject.active = false is something else entirely - it doesn't just cause a GameObject to disappear, it removes it from existence altogether. That gameobject then no longer responds to GameObject.Find calls, for instance.
Transform has no enabled property because Transform is a required component of any GameObject. A Transform is the core physical definition of the object - it is the sum of the object's position, rotation and scale, after all. It makes sense to disable the updating of an object (its script), but it makes no sense to try to disable its position, its size or its rotation.
So, what are you trying to do in your program? Make something invisible? Then set its Renderer.enabled = false. Stop something Updating? Then set the script's enabled property to false. Both? Then set both to false. ;)
Answer by kromenak · Oct 17, 2011 at 06:36 AM
Most components have an "enabled" boolean which will stop the update functions from being called for that component. There are some components, like Transform and Rigidbody, that can't be disabled like this. Setting enabled to false will stop updates from being called, but the component is still there and you can call functions in it.
It does sound like you are looking for the "active" functionality, which will put an object on death's door, so to speak, until you reactivate it. Just call gameObject.active = false.