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.
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
}
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??
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);
}
}
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?
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.
@cjdev Could You give me an example? It's a little difficult to understand what you mean.
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
Follow this Question
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