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
3
Question by superdudeman · Nov 29, 2014 at 08:06 PM · enableenable and disable script

Enabling an object that is disabled from the start?

If I create an object in the scene editor and set it to be disabled from there, is it possible to activate it from a script? It doesn't seem possible to find game objects that are already disabled as far as I can see.

Running GameObject.Find("MyDisabledObject") returns null for disabled objects when I try it. So it's not possible to run SetActive(true) on it afterwards.

Do all objects placed in the scene from the editor simply have to be enabled from the start?

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

5 Replies

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

Answer by puppeteer · Aug 26, 2015 at 12:46 PM

Inactive objects can only be "detected" in the scene through colliders.

My suggestion is to change your design so that you don't deactivate the object itself, but the components that make it visible or interactive. Disable its script if you don't want it to interact with anything, and disable its renderer if you don't want it to be visible. Also disable the collider if you don't want that to affect anything.

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
2

Answer by psycocrusher · Nov 29, 2014 at 08:29 PM

You can parent the object and run SetActive on its child.

Comment
Add comment · Show 3 · 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 superdudeman · Nov 29, 2014 at 09:08 PM 1
Share

Thanks. You mean like GameObject.Find("Parent").transform.FindChild("$$anonymous$$yDisabledObject").gameObject.SetActive(true)? It feels like a ridiculously long work around, but it works!

avatar image Westerhof superdudeman · Apr 14, 2019 at 09:31 AM 0
Share

This here saved me a lot of work, thanks buddy!

avatar image smallegduj · Aug 25, 2015 at 10:12 PM 0
Share

It's really works!!So Cool!!! $$anonymous$$any thanks for your $$anonymous$$ching!

avatar image
1

Answer by $$anonymous$$ · Nov 29, 2014 at 10:11 PM

The part of finding a disabled object i don't know, just do a public variable. And to activate the object you need to do YourObject.SetActive(true); or false if you wand to unable;

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 superdudeman · Nov 29, 2014 at 11:11 PM 0
Share

Putting an object in a public variable also works, as long as the script is not part of the same object. Thanks.

avatar image
1

Answer by ProbePLayer · May 18, 2019 at 03:22 PM

One way is to have a reference to it in the inspector before the game is started. Then you have access to it regardless whether it's disabled.

Other way is to place the deactivated object as a child of another parent (active) object, and traverse the hierarchy of that parent object with parent.GetComponentsInChildren<MyDisabledObjectScript>(true). Note the boolean argument, this has to be true to get deactivated objects as well.
If you don't have your own custom script on that disabled object, you can search for any component that your disabled object has like so: parent.GetComponentsInChildren<Rigidbody>(true), and then do a foreach loop on the returned collection to find the disabled object , like so:

    Transform[] childrenComponents = parent.GetComponentsInChildren<Transform>(true);
    foreach (Transform child in childrenComponents) {
         if (child.name == "DisabledObject") {
             // we found the desired object
            return child.gameObject;
         }
     }



And yet another way is to use the Resources.FindObjectsOfTypeAll() function. This function will find all objects of a specified type, even deactivated ones. After you get the objects of the desired type, you can then search the collection for the desired object. WARNING: this function also gets assets from the Asset folder (meaning all assets), so beware if you manipulate the returned results during runtime, you might modify an asset.

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
0

Answer by kylebarker82 · Aug 26, 2015 at 08:25 PM

are you disabling a collider, or deactivating an object? ActiveInHierarchy sounds like what you are looking for, but you're using the word disabled, which is for making a collider unusable, or turning off physics at some point in time.

if (MyDisabledObject.ActiveInHierarchy == false) { MyDisabledObject.SetActive (true); }

if your "disabled" object's name is MyDisabledObject, it's going to look for it by it's name, then turn it active, Write a coroutine if you need the inactive object to wait a while until it becomes visible in the scene.

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

31 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 avatar image avatar image avatar image

Related Questions

Enable first person controller after a sequence 0 Answers

Problems enabling and disabling a script 1 Answer

Prefab disables a script out of runtime. 0 Answers

Can't Get .enabled to Work for Children Sprites 2 Answers

(Solved!) OnEnable spams error messages 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