• 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
0
Question by Doghelmer · Jan 30, 2018 at 09:00 AM · gameobjecteditorgameobject.active

Determining if a gameobject is inactive within prefab in files, NOT in a scene

Typically when running a scene, you'd determine if a gameobject is inactive by using gameobject.activeself.

However, this doesn't seem to work when checking objects that are nested in prefabs within my files (meaning I'm not running the game and the prefab is not within my scene view, just in my files). gameobject.activeself will always return true in this case, even if the active checkbox has been unchecked for that gameobject. Is there some alternative method of checking this that I'm not aware of?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by meat5000 · Jan 30, 2018 at 10:06 AM

Um what? All GameObjects in a prefab are Inactive. Instantiate the prefab and its objects become active but its no longer a prefab; its in the hierarchy.

https://forum.unity.com/threads/no-gameobject-active-in-a-prefab.12298/

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 Doghelmer · Jan 30, 2018 at 10:23 AM 0
Share

$$anonymous$$aybe I didn't explain this well enough--

The problem is this: If I use "gameobject.activeself" to check whether an object nested within a prefab within my files is active or not (by "active" I mean the checkbox at the top-left in the inspector when viewing a gameobject), the value will always return true, regardless of whether or not the checkbox is checked.

I don't care about instantiating the prefab, I'm trying to see the "active" status within my files, when I'm not running the game. The prefab and nested objects are not in my scene view, just in my files.

Also just an aside, this post you linked seems.. incorrect? You CAN set whether or not a child of a prefab is active, and this will carry through when the prefab is instantiated.

avatar image meat5000 ♦ Doghelmer · Jan 30, 2018 at 10:33 AM 0
Share

https://answers.unity.com/questions/1266169/check-if-game-object-is-active-in-hierarchy-from-a.html

avatar image meat5000 ♦ Doghelmer · Jan 30, 2018 at 10:44 AM 0
Share

https://forum.unity.com/threads/game-objects-activeself-true-while-activeinhierarchy-false-but-no-parent.438903/

This one is an interestnig read for you.

avatar image
0

Answer by yummy81 · Jan 30, 2018 at 03:50 PM

I'm not sure if this is what you are looking for. This piece of code enumerates each and every gameobject in a given prefab with the information about whether it is active or not (GameObject.activeSelf). Paste that code somewhere in your script. It will add menu item "Test/Test" to the menu bar in the editor. You have to specify the location of your prefab and put it in the variable "path". In my example I assumed that the prefab1.prefab is in the Assets folder.

 string path = "Assets/prefab1.prefab";
 

The code works like this: it clears the console. Next, it instantiates the prefab and enumerates gameobjects with their corresponding local active state in the console. Afterwards, it destroys the instantiated object of your prefab.

 [UnityEditor.MenuItem("Test/Test")]
 private static void Test()
 {
     System.Type logEntries = System.Type.GetType("UnityEditor.LogEntries, UnityEditor.dll");
     System.Reflection.MethodInfo clear = logEntries.GetMethod("Clear", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
     clear.Invoke(null, null);
     
     string path = "Assets/prefab1.prefab";
     Object prefab = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject));
     if (prefab==null)
     {
         Debug.Log("There is no prefab at the specified location.");
         return;            
     }
     GameObject g = Instantiate(prefab as GameObject);
     Rec(g.transform);
     GameObject.DestroyImmediate(g);
 }
 
 private static void Rec(Transform t)
 {
     Debug.Log(t.name + ": " + t.gameObject.activeSelf);
     int length = t.childCount;
     for (int i = 0; i < length; i++)
     {
         Rec(t.GetChild(i));
     }
 }
 

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 Doghelmer · Jan 31, 2018 at 09:32 AM 0
Share

Thanks, I'll give this a try. A little roundabout but if it works, it works!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Any way to lock an object from changes in the editor? 4 Answers

in-editor cloning of game objects 1 Answer

Get the name of an instance's prefab at runtime? 0 Answers

How would you override Editor object dragging? 0 Answers

Canvas shrinks over time 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