- Home /
Objects not disabling on second try
I created a script where if you enter a trigger and press 'e' your player body would be disabled after 5 seconds so a blink animation could play. Once those 5 seconds finish, the main camera which is a child of the parent body is disabled and another camera located under the bed is set active. Once under the bed, the player is able to turn his flashlight on and off by pressing 'f' and if he presses 'e' again, another 5-second animation plays and then the camera under the bed is disabled and the main camera is set active again. The script works fine for only one bed, however, i made 4 beds using the same script. If I went to bed 1 and pressed 'e', the script would work fine. If I went to bed 2 and press 'e' I would be able to go under the bed but if I press 'e' again, the animation won't play and the second camera won't be disabled.
Video showing it: https://www.youtube.com/watch?v=OcTdnLIbsiI
Script: {
public GameObject Camera;
public GameObject Camera2;
public GameObject panel1;
public GameObject panel2;
public GameObject blink1;
public GameObject blink2;
public Animator _animator;
public Animator _animator1;
private bool _isUnderBed = false;
private bool _isInsideTrigger = false;
public int speed = 1;
public GameObject body;
// Use this for initialization
void Start () {
Camera.SetActive(true);
Camera2.SetActive(false);
panel1.SetActive(false);
panel2.SetActive(false);
blink1.SetActive(false);
}
void OnTriggerEnter(Collider other) { if(other.tag == "Player") { _isInsideTrigger = true; panel1.SetActive(true); } } void OnTriggerExit(Collider other) { if(other.tag == "Player") { _isInsideTrigger = false; panel1.SetActive(false); }
}
private bool Ispanel1Active
{
get
{
return panel1.activeInHierarchy;
}
}
private bool Ispanel2Active
{
get
{
return panel2.activeInHierarchy;
}
}
void UnderBed()
{
StartCoroutine(Blink());
}
IEnumerator Blink()
{
yield return new WaitForSeconds(speed);
Camera.SetActive(false);
Camera2.SetActive(true);
panel2.SetActive(true);
}
void OutofBed()
{
StartCoroutine(End());
}
IEnumerator End()
{
yield return new WaitForSeconds(speed);
Camera.SetActive(true);
Camera2.SetActive(false);
blink1.SetActive(false);
body.SetActive(false);
}
// Update is called once per frame
void Update()
{
if (Ispanel1Active && _isInsideTrigger)
{
if (Input.GetKeyDown(KeyCode.E))
{
panel1.SetActive(false);
_isUnderBed = true;
if (_isUnderBed)
{
UnderBed();
blink1.SetActive(true);
blink2.SetActive(false);
}
}
}
if (Ispanel2Active && _isUnderBed)
{
if (Input.GetKeyDown(KeyCode.E))
{
panel2.SetActive(false);
OutofBed();
blink2.SetActive(true);
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612154937im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how toDisable the FPSInputController. 1 Answer
unity 4.6 UI disabling click/touch detection on objects behind user interface 0 Answers
Why can't I disable multiple components? 1 Answer
Disabling SmoothMouseLook on pause (NullReferenceException) 3 Answers
How to prevent a script on a disabled object from firing? 0 Answers