Object instances

Hello. I am working on an open/close system for chests and the problem i found was that if i have multiple chests. Opening/closing one triggers all of them. Any way to bypass it?
I have a chest prefab that i manually added to the sene multiple times.

Here is my chest script attached to them if it helps.

//needs cleanup

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[RequireComponent(typeof(BoxCollider))]
[RequireComponent(typeof(AudioSource))]

public class Chest : MonoBehaviour {

GameObject cameraGO;

public enum State {
	open,
	close,
	between
}

public State state;
public string key;
public AudioClip openSound;
public AudioClip closeSound;
public GameObject[] components;
private Color[] _defaultColors;

public GameObject openEffect;

public Shader defaultShader;
public Shader outlineShader;

public float outlineSize = 0.01f;
public Color outlineColor = Color.black;

public GameObject gui;
public float maxRange = 3;
private GameObject _player;

// Use this for initialization
void Start () {

	defaultShader = Shader.Find("Diffuse");
	outlineShader = Shader.Find("Outlined/Silhouetted Diffuse");

	cameraGO=GameObject.FindGameObjectWithTag("MainCamera");
	state = Chest.State.close;
	openEffect.SetActive(false);

	_defaultColors =  new Color[components.Length];

	if (components.Length > 0)
					for (int i = 0; i< _defaultColors.Length; i++) {
		_defaultColors <em>= components*.renderer.material.GetColor("_Color");*</em>

* }*

* }*

* // Update is called once per frame*
* void Update () {*
* if(Input.GetKeyDown(key)){*
* chestInteraction(3);*
* }*
* visibleContainer (10);*
* }*
* public void chestInteraction(float range){*
* Ray ray = new Ray(cameraGO.transform.position, cameraGO.transform.forward);*
* RaycastHit hit;*

* if (Physics.Raycast (ray, out hit)) {*

* if(hit.distance<range){*
* if(hit.transform.tag == “Chest”){*
_ Debug.DrawLine(ray.origin,ray.origin+( ray.directionhit.distance),Color.red,2);_
_
//Debug.Log(“Chest Found”);_
_
switch(state){_
_
case State.open:_
_
state = Chest.State.between;_
_
StartCoroutine(“CloseContainer”);_
_
break;_
_
case State.close:_
_
state = Chest.State.between;_
_
StartCoroutine(“OpenContainer”);_
_
break;_
_
}*_

* }*
* }*

* }*
* }*
* public IEnumerator OpenContainer(){*
* Debug.Log(“Chest Opened”);*
* openEffect.SetActive (true);*
* audio.PlayOneShot (openSound);*
* yield return new WaitForSeconds(3);*
* state = Chest.State.open;*
* }*
* public IEnumerator CloseContainer(){*
* Debug.Log(“Chest Closed”);*
* openEffect.SetActive (false);*
* audio.PlayOneShot (closeSound);*
* yield return new WaitForSeconds(3);*
* state = Chest.State.close;*
* }*
* public void visibleContainer(float range){*

* Ray ray = new Ray(cameraGO.transform.position, cameraGO.transform.forward);*
* RaycastHit hit;*

* if (Physics.Raycast (ray, out hit) && (hit.distance < range) && (hit.transform.tag == “Chest”) ) {*
_ Debug.DrawLine (ray.origin, ray.origin + (ray.direction * hit.distance), Color.red, 2);_
* //Debug.Log (“Chest Found”);*
* highlightContainer (true);*
* } else{*
* highlightContainer (false);*
* //Debug.Log (“Chest lost”);*
* }*

* }*
* private void highlightContainer(bool highlight){*
* if (highlight) {*
* if (components.Length > 0)*

* for (int i = 0; i< defaultColors.Length; i++) {
components.renderer.material.SetColor("Color",Color.red);*
components*.renderer.material.shader = outlineShader;*

components*.renderer.material.SetFloat("Outline", outlineSize);
components.renderer.material.SetColor("OutlineColor", outlineColor);*
* }*

* } else {*
* if (components.Length > 0)*
* for (int i = 0; i< defaultColors.Length; i++) {
components.renderer.material.SetColor(“_Color”,_defaultColors);*

components*.renderer.material.shader = defaultShader;
}
}
}*_

}

The problem is you are not looking for ‘this specific chest’ to be hit. Global events like key presses will be seen by all scripts and instances. And the camera seeing something that has a tag ‘chest’ will also be ‘seen’ by all instances. You need to check that the camera ray is hitting ‘this’ chest and change it’s state only in that case.

Hmm i see what you mean but how do i check that it’s hitting that specific chest? By using getcomponent? I’ve tried but it doesn’t seem to work. Any additional info would be appreciated.

Edit: I’ve figured it out. I just replace the checking of the tag “chest” to something like this :

(hit.transform == gameObject.transform)

Thanks for the answer!