Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
4
Question by Bovine · Feb 18, 2011 at 11:35 AM · gameobjectfindactiveinactivehidden

How to find Inactive GameObject

I Have number of objects within the scene that conform to the naming convention:

male_portrait_

Where n is the number of that piece of portrait geometry. When the player clicks on a collider around the portraits, I am activating the next portrait and deactivating the current.

Given that only one can be shown at a time, I've been deactivating all but the first and then in code I was using GameObject.Find("/male_portrait_"), but I was surprised to find that this did not find inactive objects.

I was wondering if there was a way of finding inactive objects?

NOTE: It occurs to me now that I could have turned the mesh renderer and toggle this state in code as the player clicks and still be able to find the objects as I tried to above.

NOTE: my solution was to fix my code so that the objects were all active and I then deactivated the ones that should be hidden.

I'll post solution(s) separately.

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 cregox · Sep 19, 2012 at 04:09 PM 0
Share

This question hardly needed any body text at all. Title is quite self explanatory and current body gives almost no extra information! :P

16 Replies

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

Answer by Bovine · Feb 19, 2011 at 11:48 PM

Okay, so I have two solutions:

Solution 1 I left everything active, discovered all the items programatically and turned off the ones I didn't need.

Solution 2 I could hide the mesh renderer in the editor and discover the objects as above.

The confusing thing for me is that within the editor I can drag an inactive object onto the member variable of another script, thereby linking that inactive instance to that script, but I cannot do the same programmatically.

Comment
Add comment · Show 4 · 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 doomprodigy · Feb 20, 2011 at 10:38 PM 0
Share

Edited my answer with another method.

avatar image cregox · Sep 19, 2012 at 04:11 PM 0
Share

Honestly, this is not even an actual answer (to the title, at least)! Please, see my answer there (or here, in case the previous link doesn't work).

avatar image Noob_Vulcan · Jul 22, 2015 at 02:44 PM 1
Share

you can find more here http://www.unityrealm.com/how-to-find-inactive-gameobject-in-unity/

avatar image osamansr2o1oo · Sep 26, 2015 at 02:24 PM 0
Share

LOL yea it get on my nerve every time x'D u must make a reference and assign it from the inspector , damn! but i like ur idea (Solution 1) (Y)

avatar image
12

Answer by Simone · Feb 22, 2011 at 01:15 PM

I agree it is rather confusing. I expect I can FIND an inactive GameObject

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
3

Answer by cregox · Sep 19, 2012 at 04:04 PM

Recursive for the win!

The idea here is using either a non-ideal way of getting all root `GameObjects` or Tagged objects, as a place where to begin finding. Specially on the first way, this should be slower than regular `GameObject.Find`, but it will go through inactive objects.

     static public GameObject GameObjectHardFind (string str) {
         GameObject result = null;
         foreach ( GameObject root in GameObject.FindObjectsOfType(typeof(Transform)) ) {
             if (root.transform.parent == null) { // means it's a root GO
                 result = GameObjectHardFind(root, str, 0);
                 if (result != null) break;
             }
         }
         return result;
     }
     static public GameObject GameObjectHardFind (string str, string tag) {
         GameObject result = null;
         foreach ( GameObject parent in GameObject.FindGameObjectsWithTag(tag) ) {
             result = GameObjectHardFind(parent, str, 0);
             if (result != null) break;
         }
         return result;
     }
     static private GameObject GameObjectHardFind (GameObject item, string str, int index) {
         if (index == 0 && item.name == str) return item;
         if (index < item.transform.childCount) {
             GameObject result = GameObjectHardFind(item.transform.GetChild(index).gameObject, str, 0);
             if (result == null) {
                 return GameObjectHardFind(item, str, ++index);
             } else {
                 return result;
             }
         }
         return null;
     }


Keep in mind this is compatible with Unity 4 (just tested it on beta 7) but should still be avoided and it is even more unnecessary, due to how U4 now handles active and inactive game objects.

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 doomprodigy · Feb 18, 2011 at 01:29 PM

If I get what your saying. You want to disable a object and enable the rest. use SetActiveRecursively if true it will enable the item/activate and that object children aswell. if false it will deactive the object and it's children.

If you want to only disable one part of the object use active.

Heres an example. C#

public GameObject currentlyenabled = GameObject.FindWithTag("enabled"); public GameObject currentlydisabled = GameObject.FindWithTag("disabled");

//After your collision script enter this currentlyenabled.SetActiveRecursively(false); currentlydisabled.SetActiveRecursibely(true);

JavaScript

var currentlyenabled = GameObject.FindWithTag ("enabled"); var currentlydisabled = GameObject.FindWithTag ("disabled");

//After your collision script enter this currentlyenabled.SetActiveRecursively(false); currentlydisabled.SetActiveRecursibely(true);

Using the tags keep it a bit tidy compared to having multiple game objects going on and off just tag the one enabled with "enabled" and all the ones that are disabled currently "disabled".

If your asking how you can find and turn something on/off that should work. If your asking to find something inactive and access scripts from I am fairly sure you cannot do that, but what I would do in that case is turn that objects render off.

I am a bit stumped on what your asking.

Peace,

EDIT: Did you test it with the tag method that I posted before instead of just using Gameobject.Find ? If that does not work another method would be to make a Gameobject variabled in which you then manually put what object it is into the inspector and then SetActiveRecursively it (this will work).

Example C#:

public GameObject male_portrait;

male_portrait.SetActiveRecursively(true);

Example Java:

var male_portrait : Gameobject;

male_portrait.SetActiveRecursively(true);

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 Bovine · Feb 19, 2011 at 11:38 PM 0
Share

Thanks for the detailed answer - I think my description might need clarifying. I am using SetActiveRecursively() but the problem is not activating or deactivating... I'll edit my question.

I've solved this by having the items active at start and deactivating those that should be hidden.

avatar image
1

Answer by Owen-Reynolds · Sep 19, 2012 at 08:39 PM

Can set up an array at the start with links to them all. Then you never need to run a `Find` while running, so no worries about inactive. That also puts all the naming stuff in one place.

 public GameObject[] Portrait;
 // maybe set size in the inspector and drag all in, or:

 void initPortraits() {
   Portrait = new GameObject[10];
   string pBase = "male_portrait_";
   for(int i-0;i<10;i++) {
     Portrait[i] = GameObject.Find(pBase+i); // Ex: male_portrait_0
     Portrait[i].active=false;
   }
 }

The regular code can now say things like: `Portrait[curPort].active=false; curPort++; Portrait[curPort].active=true;`

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 cregox · Sep 19, 2012 at 09:05 PM 0
Share

This is actually a good idea, and most of the times a better way to think. It's also basically how he already "solved" the issue (making all objects active to begin with). But those methods don't address directly the point made in question's title.

  • 1
  • 2
  • 3
  • 4
  • ›

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

16 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

Related Questions

GameObject.Find() work on inactive objects 16 Answers

Toggling a game object between an active and inactive state 1 Answer

Activate gameobject that is deactivated with c# 2 Answers

Best way to keep track of inactive GameObjects? 1 Answer

Organize around inactive GameObjects not being findable 0 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