2
\$\begingroup\$

I want my button to activate an inactive ui image in the hierarchy through c# called "Responded_A" when the "Response_A" button is clicked. I assume this will use setActive in someway, but I'm just starting out and a little lost. I think some guidance here could really set me in motion. Thanks!

\$\endgroup\$
4
\$\begingroup\$

You can achieve this without any code using the UI

Add a additional slot to your button click event and drag the Image to the slot.

Make sure editor and runtime is set.

Then simply scroll to GameObject.SetActive

Checking the box will enable it when you click. and no check in the box will disable it when clicked enter image description here

\$\endgroup\$
2
\$\begingroup\$

This should do the trick. Add a new script to the button and put this in it. Next you'll want to drag the image into the GameObject field in the inspector. Then assign the Button component of the same object into the appropriate field.

using UnityEngine;
using UnityEngine.UI;

public class PushButton : MonoBehaviour {

    // Assign both of these in the editor.
    public GameObject image;
    public Button button;

    void Start() {
        button.onClick.AddListener(() => {
            image.SetActive(true);
        });
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.