I’m not sure where I’m going wrong, I’ve tried to create a script that toggles a separate Canvas to the Canvas my main menu buttons are on. Toggling the Canvas isn’t an issue, but to make the buttons of the ‘submenu’ canvas not navigable to with keyboard or controller I added to the script:
function Update () {
var submenuButtons = submenuCanvas.GetComponentsInChildren.<UnityEngine.UI.Button>();
if (toggled == true) {
submenuCanvas.enabled = true;
submenuButtons.enabled = true;
}
if (toggled == false) {
submenuCanvas.enabled = false;
submenuButtons.enabled = false;
}
}
the problem with this is that what ever I put behind submenu.Buttons, be it enabled, intractable, madeofchocolate, active, etc… turns up as not a member of UnityEngine.Ui.Button.
For now I’m not going to include the submenuButtons lines, as without them the script does half the job …as long as people don’t navigate left and right before the submenuCanvas is enabled. I’m just confused as to why this isn’t working.
It does seem that using GetComponentInChildren (removing the S) makes it work for the first Button. Now need to get it to work for all the buttons of the Canvas. …but this also confuses me more…
Watch what your doing in your code. Using GetComponentsInChildren.() returns an ARRAY of that type that you passed in.
So when you use the variable without the index of the single object you want to interact with to the array, it’s trying to apply it to the variable of wrong type. Either iterate over the array and pick and choose, or do it to each button(in this case) in the array.
This is an example of a for each loop, like for each Button type in the array/list/collection do this…
function Update () {
var submenuButtons = submenuCanvas.GetComponentsInChildren.<UnityEngine.UI.Button>();
if (toggled == true) {
submenuCanvas.enabled = true;
// go through each item in the array by index
for (var button:UnityEngine.UI.Button in submenuButtons) {
button.enabled = true;
}
}
if (toggled == false) {
submenuCanvas.enabled = false;
for (var button:UnityEngine.UI.Button in submenuButtons) {
button.enabled = false;
}
}
}
Perhaps using a for loop, in this case it will put the min value of 0(since arrays are zero based) in the var i per for loop and continue to apply it to the button index to disable or enable the button until it has hit the length, or max Button type in the array.
function Update () {
var submenuButtons = submenuCanvas.GetComponentsInChildren.<UnityEngine.UI.Button>();
if (toggled == true) {
submenuCanvas.enabled = true;
// go through each item in the array by index
for (var i:int = 0; i < submenuButtons.length; i++) {
button*.enabled = true;*
}*
}*
if (toggled == false) {*
submenuCanvas.enabled = false;*
for (var i:int = 0; i < submenuButtons.length; i++) {*