I’ve created a script to open and close drawers in my game but it doesn’t seem to be working.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Drawers : MonoBehaviour {
public GameObject drawer1;
public Animator drawer1anim;
public Collider drawer1coll;
public GameObject drawer2;
public Animator drawer2anim;
public Collider drawer2coll;
public GameObject drawer3;
public Animator drawer3anim;
public Collider drawer3coll;
public GameObject drawer4;
public Animator drawer4anim;
public Collider drawer4coll;
public GameObject drawer5;
public Animator drawer5anim;
public Collider drawer5coll;
public GameObject drawer6;
public Animator drawer6anim;
public Collider drawer6coll;
public GameObject drawer7;
public Animator drawer7anim;
public Collider drawer7coll;
public GameObject drawer8;
public Animator drawer8anim;
public Collider drawer8coll;
public GameObject drawer9;
public Animator drawer9anim;
public Collider drawer9coll;
private void Start()
{
drawer1 = gameObject;
drawer1anim = drawer1.GetComponent<Animator>();
drawer1coll = drawer1.GetComponent<Collider>();
drawer1anim.SetBool("open", false);
drawer2 = gameObject;
drawer2anim = drawer2.GetComponent<Animator>();
drawer2coll = drawer2.GetComponent<Collider>();
drawer2anim.SetBool("open", false);
drawer3 = gameObject;
drawer3anim = drawer3.GetComponent<Animator>();
drawer3coll = drawer3.GetComponent<Collider>();
drawer3anim.SetBool("open", false);
drawer4 = gameObject;
drawer4anim = drawer4.GetComponent<Animator>();
drawer4coll = drawer4.GetComponent<Collider>();
drawer4anim.SetBool("open", false);
drawer5 = gameObject;
drawer5anim = drawer5.GetComponent<Animator>();
drawer5coll = drawer5.GetComponent<Collider>();
drawer5anim.SetBool("open", false);
drawer6 = gameObject;
drawer6anim = GetComponent<Animator>();
drawer6coll = GetComponent<Collider>();
drawer6anim.SetBool("open", false);
drawer7 = gameObject;
drawer7anim = GetComponent<Animator>();
drawer7coll = GetComponent<Collider>();
drawer7anim.SetBool("open", false);
drawer8 = gameObject;
drawer8anim = GetComponent<Animator>();
drawer8coll = GetComponent<Collider>();
drawer8anim.SetBool("open", false);
drawer9 = gameObject;
drawer9anim = GetComponent<Animator>();
drawer9coll = GetComponent<Collider>();
drawer9anim.SetBool("open", false);
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetButton("Fire1"))
{
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.CompareTag("Drawer 1"))
{
drawer1anim.SetBool("open", !drawer1anim.GetBool("open"));
}
if (hit.collider.CompareTag("Drawer 2"))
{
drawer2anim.SetBool("open", !drawer2anim.GetBool("open"));
}
if (hit.collider.CompareTag("Drawer 3"))
{
drawer3anim.SetBool("open", !drawer3anim.GetBool("open"));
}
if (hit.collider.CompareTag("Drawer 4"))
{
drawer4anim.SetBool("open", !drawer4anim.GetBool("open"));
}
if (hit.collider.CompareTag("Drawer 5"))
{
drawer5anim.SetBool("open", !drawer5anim.GetBool("open"));
}
if (hit.collider.CompareTag("Drawer 6"))
{
drawer6anim.SetBool("open", !drawer6anim.GetBool("open"));
}
if (hit.collider.CompareTag("Drawer 7"))
{
drawer7anim.SetBool("open", !drawer7anim.GetBool("open"));
}
if (hit.collider.CompareTag("Drawer 8"))
{
drawer8anim.SetBool("open", !drawer8anim.GetBool("open"));
}
if (hit.collider.CompareTag("Drawer 9"))
{
drawer9anim.SetBool("open", !drawer9anim.GetComponent("open"));
}
}
}
}
}
}
Any suggestions ???