How can I have 2 Game Objects with the same script act independently?
In an FPS setting, I have 2 buttons, they both use the same script. I have a door object which I animate using purely code by using Lerp on Vector3's. I would like for the door to have a "button requirement", as in I'd have to press a certain number of buttons around the map before it opens. I do this by setting an int variable in the Door script. Which the button script then increments when pressed.
However, when pressing one button, instead of incrementing 1, it increments the same number of buttons that exist. i.e. if there's 3 buttons in the world, pressing 1 button is the same as pressing all 3 at the same time.
Also another question which is related is that the check is something like if (Input.GetButtonDown("Use") && !isPressed)
, And then once the button is pressed I set isPressed
to true. This is also setting it true for the other buttons in the world, how would I make it apply to just the button which was pressed?
Button Script:
public class ButtonHandler : MonoBehaviour {
public GameObject TextDisplay;
public float Distance = PlayerCasting.DistanceToTarget;
public GameObject Door;
private bool isPressed = false;
// Update is called once per frame
void Update () {
Distance = PlayerCasting.DistanceToTarget;
if (Input.GetButtonDown("Use") && !isPressed)
{
if (Distance <= 5)
{
isPressed = true;
OpenDoor.ButtonsPressed += 1;
}
}
}
Door Script:
public class OpenDoor : MonoBehaviour {
public int ButtonRequirement;
public int RaiseHeight = 8;
public float speed = 0.2f;
public static int ButtonsPressed = 0;
private Vector3 _closedPosition;
private Vector3 _endPos;
// Use this for initialization
void Start () {
_closedPosition = transform.position;
_endPos = _closedPosition + new Vector3(0, RaiseHeight, 0);
}
// Update is called once per frame
void Update () {
if (ButtonsPressed == ButtonRequirement)
{
StartCoroutine("OpenTheDoor");
}
}
IEnumerator OpenTheDoor()
{
float t = 0f;
Vector3 startPos = transform.position;
while (t < 1f)
{
t += Time.deltaTime * speed;
transform.position = Vector3.Lerp(startPos, _endPos, t);
yield return null;
}
}
}
Could you post the complete script so we can better see what you are doing?
Your answer

Follow this Question
Related Questions
How to get touch's positon when I hold the button?? 2 Answers
Creating dynamically moving buttons/GUI objects. 1 Answer
How do I change color with Button onClick() 0 Answers
Next and Back Button? 0 Answers
Problem with enabling menu 0 Answers