How to make script affect only one this game object

Hello everyone.
I am 3D modeller more than a programmer. But now I need several doors in my scene to be opened and closed.

What I have. Two doors. One animator controller attached to these doors. A couple of clips placed in this animator controller, that is for opening rotation and closing rotation.
And DoorController script.

First I had an issue in which all of my doors were opened in one moment when I have triggered an OpenDoor animation. I resolve this problem by using a call to a certain object that Raycast hitting with, instead of just getting a local Animator object.
But what problem I have now is: When I open one of the doors, my program assign a string value “Opened” to doorState parameter of ALL OF THE DOORS IN THE SCENE. This makes rest of the doors playing the wrong animation after all (because they still closed in real).

The question is: How can I use my script with only one instance of a door and store unique parameter’s values for this only one instance. How can I send command in this script to only this one certain door, not to everyone that have this script attached? How can I call to Animator component of this only one instance of a door? Not to everyone that have this script attached.

public class DoorController : MonoBehaviour {

    //Animator m_animator;
    public Camera m_camera;
    public float maxDistance = 2;
    private string doorState;
    RaycastHit hit;

    // Use this for initialization
    void Start () {
        //m_animator = gameObject.GetComponent<Animator>();
        doorState = "Closed";

    }
	
	// Update is called once per frame
	void Update () {
        
        if (Physics.Raycast(m_camera.transform.position, m_camera.transform.forward, out hit, maxDistance))
        {
            if (hit.transform.tag == "Door")
            {
                if (Input.GetButtonDown("Use"))
                {
                    OpenDoor();
                }
            }
        }
	}

    void OpenDoor()
    {
        if (doorState == "Closed")
        {
            // Open a door

            //m_animator.SetTrigger("OpenDoor");
            hit.transform.GetComponent<Animator>().SetTrigger("OpenDoor");
            doorState = "Opened";
        }
        else
        {
            // Close a door

            //m_animator.SetTrigger("CloseDoor");
            hit.transform.GetComponent<Animator>().SetTrigger("CloseDoor");
            doorState = "Closed";
        }
    }
}

Hey there! The issue here is that the raycast is being performed in every Update() on every door. This means that if your main camera hits any door in the scene, each raycast from each door is going to register as having hit a door. This will, in turn, set the state of every door to “Opened”. You could either check that hit.gameObject == this, to ensure that only the door that is actually hit by the raycast is marked as “Opened”, or do as @Zodiarc says and use a collider to check whether the player is within door opening range.

Personally, I would go down the collider route as it’s simpler and doesn’t involve casting rays every frame for every door.

As a side note, it would probably be better to perform the input check in the player script rather than the door script in order to centralise your input logic. You might also like to use an enum to indicate Open and Closed states for your door rather than strings. If you haven’t used enums before it’s definitely worth learning and can make your code a bit clearer.

That’s how I would do it:

Add a collider to the player and check the isTrigger (or similar) checkmark. Then write something similar to that:

class DoorController: MonoBehaviour {

	private Animator animator;
	public bool open = false;

	public void Start() {
		this.animator = GetComponent<Animator>();
	}

	public void OnTriggerStay(Collider other) {
		if(Input.GetButtonDown("Use")) {
			open = !open;
			animator.SetBool("Open", open);
		}
	}
}

OnTriggerStay will only be called when the collider of the door is within a trigger collider, so only one door should be affected. Note: I didn’t check if it actually works. To improve it further you can also check if the other collider is the player