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 /
  • Help Room /
avatar image
0
Question by DaPizzaKing · Jan 13, 2016 at 11:53 AM · scripting problem

How do I get one object to detect whether or not another object's setActive is equal to False?

I am trying to get a door to detect when another object has been "collected" or in other words, has its' setActive bool set to FALSE. I already figured out how to get the object to disappear but when I try to get the door to open when it has been "collected", I have no clue what I am doing. I haven't come across anything that has helped, so anything helps. I appreciate it . Thank You.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by cjdev · Jan 13, 2016 at 02:15 PM

GameObject.Find() can get you a reference to your disappearing object. The ! operator is "not" which means it switches around any boolean. The boolean activeSelf is what tells you whether or not it's active. Also, if you're going to check this every frame you'll want to keep the GameObject from GameObject.Find() in a variable because it costs a lot (in terms of performance).

 if (!GameObject.Find("DisappearingObject").activeSelf)
 {
     // Open Door
 }

Comment
Add comment · Show 6 · 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 DaPizzaKing · Jan 13, 2016 at 11:32 PM 0
Share

It still doesn't seem to be working. The name I set as the "disappearing object" was "Pickup3". So this is the script I used on the door:

 using UnityEngine;
 using System.Collections;
 
 public class Door : $$anonymous$$onoBehaviour 
 {    
     void Update ()
     {
         if (! GameObject.Find("PickUp3").activeSelf) 
         {
             gameObject.SetActive (false);
         }
     }
 }


So what am I doing wrong??

avatar image cjdev · Jan 14, 2016 at 12:14 AM 0
Share

Well now that I look at it when it's not 5am I guess it's pretty tough to find a GameObject that's already disabled, sorry about that. In this case you'd probably want to switch the logic around and disable the door from the disappearing object right before it gets deactivated. There's a method that triggers right before an object gets disabled called OnDisable() so your pickup object might look like this:

  using UnityEngine;
  using System.Collections;
  
  public class Pickup3 : $$anonymous$$onoBehaviour
 {
     void OnDisable()
     {
         GameObject.Find("Door").SetActive(false);
     }
 }
avatar image DaPizzaKing · Jan 14, 2016 at 12:25 AM 0
Share

It worked! Thank You! But one more thing... Is there a way that the door will only disappear when all three collectibles have been picked up?

avatar image cjdev DaPizzaKing · Jan 14, 2016 at 02:03 AM 0
Share

For that you could set a counter variable on the Door object from your OnDisable function ins$$anonymous$$d. Then you'd check it in Update and when it hits 3 you'd call SetActive on the Door.

avatar image DaPizzaKing · Jan 14, 2016 at 07:25 PM 0
Share

@cjdev Could You give me an example? It's a little difficult to understand what you mean.

avatar image cjdev DaPizzaKing · Jan 14, 2016 at 08:53 PM 0
Share

Basically you keep track of how many pickups have been collected in the Door object, something like this:

 public class Door : $$anonymous$$onoBehaviour
 {
     public int numPickedUp = 0;
 
     void Update()
     {
         if (numPickedUp == 3)
         {
             gameObject.SetActive(false);
         }
     }
 }
 
 public class Pickup : $$anonymous$$onoBehaviour
 {
     void OnDisable()
     {
         // x++ is shorthand for x = x + 1
         GameObject.Find("Door").GetComponent<Door>().numPickedUp++;
     }
 }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why isn't my player moving down? 0 Answers

HP, DMG, HUD scripts problem 0 Answers

How to get 2D blocks to break after a certain amount of hits 0 Answers

Debugging Collision script - Help required to understand behaviour 2 Answers

Need to get the difference between the rotation of a normal and an object 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