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
1
Question by by0log1c · Oct 17, 2011 at 02:26 AM · componentactiveenabled

'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!

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 Jibidiah · Oct 17, 2011 at 02:45 AM 0
Share

I have like 20 obsolete warnings messages and yet everything works fine on $$anonymous$$e. Just try to live with the warnings?

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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;

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 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?

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 by0log1c · Oct 17, 2011 at 06:07 AM 0
Share

Because... you know... $$anonymous$$issingFieldException: Field 'UnityEngine.Transform.enabled' not found.

avatar image Tseng · Oct 17, 2011 at 12:44 PM 0
Share

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

avatar image
1

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. ;)

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 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.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Disabling GameObject doesn't disable components 0 Answers

enabled is not a member of UnityEngine.Component 1 Answer

Disable all components but 1 on a gameobject 1 Answer

Disable Object Leave Script Running 1 Answer

Disabled script still working C# 3 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