- Home /
Variable only changes for one object that has the script attached and not the other objects with the same script.
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.
Cool - waiting for it to be moderated before I can answer :)
Answer by Schytheron · Dec 23, 2015 at 04:32 PM
I solved it myself! I just had to reverse my logic. Instead of having a activeButtons int in the ButtonPress script I put it in the ButtonChecker script instead and let the ButtonPress script access to the ButtonChecker Script and let it change the activeButton variable in the ButtonChecker script when colliding with a button. TADA!
Now the ButtonPress script looks like this:
using UnityEngine;
using System.Collections;
public class ButtonPress : MonoBehaviour
{
//public GameObject TurningDoor;
//float hoverForce = 0.2f;
//bool CollidingObject = false;
public ButtonChecker ButtonCheck;
public GameObject Door;
// Use this for initialization
void Start ()
{
ButtonCheck = Door.GetComponent<ButtonChecker> ();
}
// Update is called once per frame
void Update ()
{
}
void OnCollisionEnter (Collision Other)
{
//CollidingObject = true;
ButtonCheck.activeButtons++;
}
void OnCollisionExit (Collision Other) {
//CollidingObject = false;
ButtonCheck.activeButtons--;
}
And the ButtonChecker Script looks like this:
using UnityEngine;
using System.Collections;
public class ButtonChecker : MonoBehaviour {
public int amountofButtons;
public int activeButtons = 0;
public GameObject[] buttons;
float rotationForce = 0.1f;
void Start() {
buttons = GameObject.FindGameObjectsWithTag("Button");
amountofButtons = buttons.Length;
}
// Update is called once per frame
void Update () {
Debug.Log (activeButtons);
if (activeButtons == amountofButtons) {
transform.Rotate( new Vector3(0, 5, 0) * rotationForce, Space.Self );
}
}
}