#I have an issue with buttons AddListener

1 messages · Page 1 of 1 (latest)

tough talon
#
    public Text buttonText;

    public bool opened = false;
    Animator animator;

    private void Start()
    {
        animator = GetComponentInParent<Animator>();
    }

    public void DoActionDoor()
    {
        if (this.opened) { this.animator.SetBool("open", false); this.animator.SetBool("close", true); this.opened = false; }
        else { this.animator.SetBool("close", false); this.animator.SetBool("open", true); this.opened = true; }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Player")) 
        {
            behaviourButton.SetActive(true);
            behaviourButton.GetComponent<Button>().onClick.AddListener(this.DoActionDoor); //isssue
            if (opened) { buttonText.text = "Close Door"; }
            else { buttonText.text = "Open Door"; }
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.CompareTag("Player")) { behaviourButton.SetActive(false); behaviourButton.GetComponent<Button>().onClick.RemoveAllListeners(); }
    }``` so i have this script. The logic of it is... we have a door (which has a trigger), a ui button and animator. The thing is, i want to make it like this : You get to the door trigger zone, a button shows up ("Open Door"), you press on it and the door opening animation pops up. What's the issue, is that the button doesn't add the listener to it. I have multiple doors in which i want to do this script. I would appreciate the assist!
violet sequoia
#

First off, how do you determine that the action does not get added to the listener? Actions added through code will not show up in the inspector.
Second, make sure that you are referencing the right button instance. Chances are you will want to get the reference during runtime.
Third, I would recommend making a sort of interaction manager, rather than having each door handle its interactions on its own.

tough talon
#

yeah i made a manager. So the issue now is that it shows the right object and the right animator, but now the animation is shown to be doing in the animator, but not in play