The question is answered, right answer was accepted
Bool won't change when commanded to in script
Hello, I am working on a script which when you do not have a key a bool is false. But when you have the key the bool is true. Basically when I pick the key up the bool won't change from false to true and I'm not too sure why, Here's my script:
using UnityEngine;
public class Door : MonoBehaviour
{
public GameObject key;
public Animator ANI2;
public GameObject Text;
public AudioSource dooropen;
public AudioSource doorlock;
public bool haskey;
public bool Dooropen;
void Start()
{
haskey = false;
}
void OnTriggerStay(Collider other)
{
Text.gameObject.SetActive(true);
if (Input.GetKeyDown("e"))
{
ANI2.enabled = true;
ANI2.SetInteger("Doorani", 1);
Dooropen = false;
if (haskey == true)
{
dooropen.Play();
}
}
if (Input.GetKeyUp("e"))
{
ANI2.enabled = false;
}
if (haskey == true)
{
Dooropen = true;
ANI2.SetInteger("Doorani", 2);
}
if (haskey == false)
{
Dooropen = false;
ANI2.SetInteger("Doorani", 1);
}
}
void OnTriggerExit(Collider other)
{
Text.gameObject.SetActive(false);
ANI2.enabled = false;
}
void Update()
{
if (ANI2.enabled == true)
{
Text.gameObject.SetActive(false);
}
if (Dooropen == true)
{
Text.gameObject.SetActive(false);
}
if (key == false)
{
haskey = true;
}
if (key == true)
{
haskey = false;
}
}
}
Answer by upasnavig90 · Mar 02, 2018 at 10:35 AM
isn't this code giving you any errors if (key == false) { haskey = true; } if (key == true) { haskey = false; }
because key is a gameObject here and you can not check it in if condition, if it is true or not,(how a gameobject can be true or not) may be you are trying to check if it is enabled or not, for that use "key.activeSelf"
Thank you so much, I must have had a brain fart to not think of that haha, that's what happens when I script at 3 in the morning. :)
Answer by Burnoisette · Mar 02, 2018 at 09:50 AM
@NEGATIVERAGDOLL Your comparing a GameObject (key) on true/false, this is weird. How does your player get a key? Does he collide with it or something?
if (key == false)
{
haskey = true;
}
if (key == true)
{
haskey = false;
}