Question by
WyattJ1834 · Oct 17, 2017 at 10:51 PM ·
c#programming
Unity2D Door and Button Pairing,Unity2D door and button pairing
I have two scripts. A door script and a button script.
Door (Excluding the first few lines):
void Start () {
button = FindObjectOfType<Button>();
anim = GetComponent<Animator>();
open = 1.0f;
bc2D = GetComponent<BoxCollider2D>();
}
// Update is called once per frame
void Update ()
{
//assigns the blend variable in the animator to another mutable variable
open_ = anim.GetFloat("Blend");
//Checks to see if the button is pressed
if (button.buttonCode == doorCode)
{
if (button.Pressed == true)
{
//Finds the door with the corresponding code and opens it
anim.SetBool("open", true);
bc2D.enabled = false;
if (anim.GetFloat("Blend") < open)
{
open_ += 0.05f;
anim.SetFloat("Blend", open_);
}
}
}
if (button.Pressed == false)
{
if (button.buttonCode == doorCode)
{
anim.SetBool("open", false);
bc2D.enabled = true;
if (anim.GetFloat("Blend") > 0)
{
open_ -= 0.05f;
anim.SetFloat("Blend", open_);
}
}
}
}
Button (Again excluding the first few lines):
void Start () {
anim = GetComponent<Animator>();
canTriggerButton = FindObjectOfType<CanTriggerButton>();
}
void Update()
{
Pressed = anim.GetBool("pressed");
}
void OnTriggerEnter2D(Collider2D other)
{
inside += 1;
if (canTriggerButton.canTriggerButton == true)
{
anim.SetBool("pressed", true);
}
}
void OnTriggerExit2D(Collider2D other)
{
inside -= 1;
if (canTriggerButton.canTriggerButton == true && inside==0)
{
anim.SetBool("pressed", false);
}else
{
anim.SetBool("pressed", true);
}
}
I just barely learned C# so sorry if it's really crude code. If I only have one door and one button it works fine but if I add anymore, it starts breaking. Any tips for my coding would be appreciated. Or if there is an easy fix that I just can't see please help.,
Comment