Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Borr1310 · Nov 03, 2016 at 10:15 AM · prefabhierarchyactiveaccessingfind-gameobject

Check if game object is active in hierarchy from a prefab

If I want to check if game object is active I know I can use if(GameObject.ActiveInHierarchy){...} But what I want to do now is check that from a prefab. I obviously can´t just drag and drop a game object into a prefab.. so what do I do? I did already try to use..

 FallOrFly = GameObject.FindGameObjectWithTag("FallOrFly");
         if (FallOrFly.activeInHierarchy == true)
         {....}

But it doesn´t seem to work?

Thanks.

Comment
Add comment · Show 3
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 aditya007 · Nov 03, 2016 at 11:34 AM 1
Share

Try using GameObject.activeSelf ins$$anonymous$$d.

avatar image AngryKiwiArts aditya007 · Nov 03, 2016 at 11:35 AM 0
Share

GameObject FallOrFly; public bool activeSelf; public int score;

 void SomeFunction() {
     if (FallOrFly == null)
         FallOrFly = GameObject.FindGameObjectWithTag("FallOrFly");
         if (FallOrFly.activeSelf == true)
         {
         ...
         }
     }
avatar image Borr1310 aditya007 · Nov 03, 2016 at 12:08 PM 0
Share

Tried, and it doesn´t seem to work.

2 Replies

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

Answer by Borr1310 · Nov 03, 2016 at 12:57 PM

FIXED IT!

I am sorry for not giving you all the information. The problem was that the prefab can´t detect inactive game objects so it returned null every time the game object was set to false. My way around it was to ad:

 if (FallOrFly == null)
         {
         }
         if (FallOrFly != null)
         {
             if (FallOrFly.activeInHierarchy == true)
             {
                
             }
         }

Thank you for all responses.

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 Gogoasdfg · Nov 23, 2017 at 02:14 PM 0
Share

You are awsome bro! Thanks

avatar image
0

Answer by Cynikal · Nov 03, 2016 at 11:28 AM

Your code should work just fine on your prefab. But, let's break it down Barney Style.

You're using: FindGameObjectWithTag. Are you sure it's TAGGED as such and not Named?

Are you sure the object is instantiated?

Where are you initiating this Find at? On the instantiated prefab? And if so where? In Start?

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 Borr1310 · Nov 03, 2016 at 12:06 PM 0
Share

Alright... First off let me try to explain what I´m trying to do. I have 2 "Game modes". Flying and Falling. The default game mode was flying and then I decided to add another one so the game is not that boring.. So I created an empty game object named FallOrFly. Then I have 2 buttons Fall or Fly. If you press fall everything is the same except that FallOrFly gameobject is set to inactive. Then I added an if statement in front of almost every "function" ( if FallOrFly is set to active(he pressed fly) so do everything in fly mode and if ForOrFly is set to inactive that means the user pressed fall so do everything in fall mode.. Now the only problem I am having is that I can´t add that if statement since I don´t know how to get ForOrFly gameobjects status from a prefab. And that prefab is a parent of the whole obstacle thing(2cubes,emptyspace,coins...) It is being instantiated with InvokeRepeating.

  1. $$anonymous$$y game object is tagged "FallOrFly" and I am trying to find an object with tag(FallOrFly)..

  2. The object I believe is instantiated.. ? The script is attached to the parent and the parent is being instantiated with Invoke Repeating so I believe it is instantiated..

  3. I am calling FallOrFly = GameObject.FindGameObjectWithTag("FallOrFly"); in Start() on the script attached to the parent GameObject.

avatar image Borr1310 · Nov 03, 2016 at 12:24 PM 0
Share

Also I am getting that error: Object reference not set to an instance of an object, I believe that means that the instantiated prefab cant find the gameobject...

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

71 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

Related Questions

Why cannot I drag and drop a gameObject from Hierarchy to a prefab's slot in the Inspector? 3 Answers

Need to see the whole hierarchy of a Prefab in the Project Tree 3 Answers

SetActive() not working 0 Answers

Instantiate a prefab inside a hiearcy 1 Answer

NavMesh and WaveSpawner dont work together! 2 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