how do i change a property from a parent object in script?
i have a button and i want to change it's interactable value through it's text component in c# script. how do i do that?
in a unity tutorial there was a line that didn't work: buttonList[i].GetComponentInParent().interactable = false;
buttonList is of type Text[]
Answer by tormentoarmagedoom · May 13, 2018 at 09:30 AM
Good day,.
I think you are little lost..:D
first, buttonList[i] is refered to the element number "i" of the buttonList array.
to modify if a button is intractable, you need to access the component Button of the gameobject Button, with
GetComponent<Button>().interactable = false;
But first you need to reach the object.
To get the parent of an object, you need to acces it via it transforms. If Hand is a child of Body (imagine Body is the button), we can say this
Hand.transform.parent.gameObject.GetComponent<Button>().interactable = false;
And now, if you need to know the Text value to decide if is interactabel or not, you need to read the text value. Text is a component from a object, and Text.text is the property of Text component, the writen words in this Text component. (there are other properties, like text size, text color, etc...)
TheTextObject.GetComponent<Text>().text = "The text words";
Remember that to code with UI elements, you need to add this library at the beggining of your script!
using UnityEngine.UI;;
Bye!
Your answer
Follow this Question
Related Questions
How do I catch a click on a non-interactable button? 1 Answer
Steam VR Snap Turn while holding Throwable object not working 2 Answers
How to set button not interactable without dim? 1 Answer
button.interactable = false; not working ,Button.interectable = false; doesnt work 0 Answers
Disabling UI while audio is playing 0 Answers