I have a bool value hasKey[×] & I can’t get its value to change.
I’ve stared at this for hours and I’m entirely at a loss for what to do.
Control:
public class Control : MonoBehaviour
{
[HideInInspector]
public bool flashlight, flashdrive, disk, newHouse, xMas95, goldfinchVHS;
[HideInInspector]
public bool[] hasKey;
[HideInInspector]
public List<int> locked;
protected virtual void Awake()
{
hasKey = new bool[30];
locked = new List<int> { 0, 3, 8, 9, 10, 13, 14, 15, 16, 17, 18, 19, 20, 24, 25, 28, 29 };
for (int i = 0; i < hasKey.Length; i++)
{
hasKey[i] = !locked.Contains(i);
}
}
private void Start()
{
flashlight = false;
flashdrive = false;
disk = false;
newHouse = false;
xMas95 = false;
goldfinchVHS = false;
}
public bool CheckCollision(Collider obj)
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (hit.transform == obj.transform)
{
return true;
}
else return false;
}
else return false;
}
}
GettingKey:
public class GettingKey : Control
{
public static GettingKey key;
Collider KeyColl;
public int KeyNumber;
public void Start()
{
KeyColl = gameObject.GetComponent<Collider>();
}
void Update()
{
if (Input.GetButton("Fire1"))
{
if (CheckCollision(KeyColl))
{
hasKey[KeyNumber] = true;
gameObject.SetActive(false);
}
}
}
}
DoorOpenCloseControl:
public class DoorOpenCloseControl : Control
{
public static DoorOpenCloseControl control;
Animator doorAnim;
public int doorNumber;
Collider doorColl;
public AudioSource DoorLocked;
void Start()
{
doorColl = gameObject.GetComponent<Collider>();
doorAnim = gameObject.GetComponent<Animator>();
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
if (CheckCollision(doorColl))
{
if (hasKey[doorNumber]) //evaluates as its original setting even if I've changed the value in GettingKey
{
doorAnim.SetBool("open", !doorAnim.GetBool("open"));
}
else
{
DoorLocked.Play();
}
}
}
}
}