script to open/close drawers

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 ???

It could be that you are accepting the Input from OnTriggerEnter instead of in an Update method.

Move the Raycast and Input.GetButton into a Update method.

Just some things to adjust.

Also, please use arrays and for loops.

Edited: Updated thanks to Timelog.

Ex:

// Assigned Drawers in the Inspector
[SerializeField]
private GameObject[] drawers;
private Animator[] drawerAnims;
private  Collider[] drawerColls;

void Start(){
    drawerAnims = new Animator[drawers.Length];
    drawerColls = new Collider[drawers.Length];

    for(int i = 0; i<drawers.Length; i++){
        drawerAnims[i] = drawer[i].GetComponent<Animator>();
        drawerAnims[i].SetBool("open", false);
        drawerColls[i] = drawer[i].GetComponent<Collider>();
    }
}

Change all of the drawers to use 1 tag - “drawer”
Edit: I cleaned up the code. Drop in the drawers you already created, and set all the drawer tags to just “drawer”.

using UnityEngine;

public class Drawers : MonoBehaviour {
    // Drag and drop the draws in the inspector.
    [SerializeField]
    private GameObject[] drawers;

    void Update() {
        if (Input.GetButtonDown("Fire1")) {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit)) {
               // All drawers should have the tag drawer.
                if (hit.collider.CompareTag("drawer")) {
                    Animator anim = hit.collider.gameObject.GetComponent<Animator>();
                    anim.SetBool("open", anim.GetBool("open"));
                }
            }
        }
    }
}
2 Likes

What a mess.
I will post again at some point with an improvement.

When you see code being duplicated like this there is ALWAYS an easier way.

If no access modifier is given the method is private by default, eg. ‘void Start()’ and ‘private void Start()’ have exactly the same level of access, namely ‘private’.

To OP:
It seems like on your code all drawers get assigned the gameobject that the '‘drawers’ object is attached to, instead of each having an unique object:

drawer1 = gameObject;
drawer2 = gameObject;
drawer3 = gameObject;

Remove those lines from Start() if you assign the unique drawers in the inspector because they will currently be overridden. Also, use the code provided by @Simple-Blue

1 Like