I have 3 big buttons in my scene that are placed on the floor. Each button is a prefab and therefore the same. Each button has a script that checks if something collides with the button. I then have a door with a script attached called ButtonChecker which checks if all buttons are colliding with something. If they are, the door should spin around (its a spinning door).
Here is the script thats attached to each button:
public class ButtonPress : MonoBehaviour
{
//public GameObject TurningDoor;
//float hoverForce = 0.2f;
//bool CollidingObject = false;
public int activeButtons = 0;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
void OnCollisionEnter (Collision Other)
{
//CollidingObject = true;
activeButtons++;
}
void OnCollisionExit (Collision Other) {
//CollidingObject = false;
activeButtons--;
}
And here is the script that is attached to the door:
public class ButtonChecker : MonoBehaviour {
public int amountofButtons;
public GameObject FirstButton;
public ButtonPress ButtonPresser;
public GameObject[] buttons;
float rotationForce = 0.1f;
void Start() {
ButtonPresser = FirstButton.GetComponent<ButtonPress> ();
buttons = GameObject.FindGameObjectsWithTag("Button");
amountofButtons = buttons.Length;
}
// Update is called once per frame
void Update () {
Debug.Log (buttons.Length);
if (ButtonPresser.activeButtons == amountofButtons) {
transform.Rotate( new Vector3(0, 5, 0) * rotationForce, Space.Self );
}
}
}
The problem is that the Int “activeButtons” in the ButtonPress script is only adds for one button. Example: If I stand on Button 1, activebuttons value will be set to 1. If I then put a cube on Button 2 (while standing on Button 1) , activeButtons value wont change to 2.