Question by
andypan · Feb 26, 2017 at 09:21 AM ·
uibuttonbutton trigger events
Change multiple button interactable fields
My program has a lot of buttons (over 300) and their interactable states all vary (some are enabled, some are disabled). I'm trying to write a script that will enable every single button by setting objects tagged ButtonTag1 interactable to True. I'm using the event system on a button to enable the public void "EnableButton" which should enable every single button, but when I run the function I get this error:
NullReferenceException: Object reference not set to an instance of an object ButtonEnable.EnableButton () (at Assets/_Scripts/ButtonEnable.cs:16)
And here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonEnable : MonoBehaviour {
public GameObject[] objs;
void Start() {
objs = GameObject.FindGameObjectsWithTag("ButtonTag1");
}
public void EnableButton() {
objs = GameObject.FindGameObjectsWithTag("ButtonTag1");
foreach (Object ButtonTag1 in objs) {
GetComponent<Button>().interactable = true;
}
}
}
Comment
Best Answer
Answer by andypan · Feb 26, 2017 at 11:53 AM
This problem has been solved so I'll share the solution in case anyone comes across a similar situation.
I replaced
foreach (Object ButtonTag1 in objs) {
GetComponent<Button>().interactable = true;
}
with
foreach (GameObject ButtonTag1 in objs) {
ButtonTag1.GetComponent<Button>().interactable = true;
}