- Home /
Unity - destroy clone child object (button)
I would like to create a new button object with text and add it to scrollview, each time when is default button pressed. If the new button from the scrollview were pressed, It would delete itself.
public InputField Input;
public GameObject itemTemplate;
public GameObject content;
public void addButton_Click(){
StringInput = Input.text;
var copy = Instantiate(itemTemplate);
copy.transform.parent = content.transform;
copy.GetComponentInChildren<Text>().text = (StringInput).ToString();
Input.text = ("");
copy.GetComponent<Button>().onClick.AddListener( ()=> {
//Onclick method
});
}
Comment
Best Answer
Answer by EpicAsMe · Jan 09, 2020 at 07:56 AM
Try to call Destroy(copy.gameObject)
inside onClick delegate
Your answer
Follow this Question
Related Questions
Add Listeners to array of Buttons 2 Answers
Clicking again does not place object 1 Answer
HELP how to use buttons 1 Answer
Unity 5: UI button OnPointerDown not function as expected 1 Answer