- Home /
Adress canvas group within a clone of prefab
I have a group of UI elements as a prefab, i am rendering/cloning this prefab in a ScrollView an x amount of times from a loop. The prefab has many children, one of which is a Panel with the CanvasGroup component.
I have a button (also a child of the same prefab) and i have an event listener on it.
newInventoryItem.GetComponent<Button>().onClick.AddListener(() => ItemMenuOpener());
Inside the ItemMenuOpener() i need to address and change the parameters of the CanvasGroup component of the panel in the same prefab on which the button has been clicked. How would that be possible? Thank you in advance!
Answer by NikLaws · Mar 09, 2019 at 07:56 PM
After a lot of experimentation i found a solution. It turned out to be far simpler than i expected:
newInventoryItem.GetComponent<Button>().onClick.AddListener(() => CardMenuOpener(newCard.GetComponentInChildren<CanvasGroup>()));
I'm simply passing down the component to the Event Listener.
Answer by badadam · Mar 09, 2019 at 04:56 PM
Try this code
newInventoryItem.transform.Find("buttonName").GetComponent<Button>().onClick.AddListener(()=> ItemMenuOpener());
Thanks for the answer, but i believe you misunderstood my question. The event listener is working fine, what i don't understand is how to address the canvasGroup component in the same prefab clone as the button which is clicked.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
How to instantiate a prefab without showing it straight away 1 Answer
ridgidbody 2d update rate? 0 Answers
Clone of prefabs copy original´s script state during runtime? 0 Answers