Buttons won't get interaction in instantiated prefab
Hello,
I'm working on having jobs assigned to characters through a roster. I've made an array of units, and for each of them within the bounds that fits in my roster I'm instantiating a panel. On each panel there is a button that I've attached a simple script:
public class Jobs : MonoBehaviour {
public Roster charRost;
public void JobAssign()
{
Debug.Log( " assigned to Job:" + charRost.selectedID);
}
}
where under my Roster class there's a character assignment function that works by clicking on the panel in the roster and returning the ID of the character for that panel.
I have several different panels serving as the menus, and have been able to have interaction with generic buttons and with non instantiated versions of the prefab. For a reason that I don't understand, I'm unable to get any sort of interaction with the button on the prefab ( no highlights, clicks or selects).
I've tried to get the button behavior both with OnClick() and with an Event Trigger from selection, with a prefab object with the above script in the object spot. There's an Event System in my scene and I don't think that there's a panel in the way; as I've tried not instantiating my array of prefabs and manually setting one of these where the prefabs would be instantiated and been able to interact with the button, which makes me think that it may be something to do with how I'm instantiating the prefabs.
public void PopulateRosterPanel(int start, int last)
{
panelPos = new Vector3(392, 530, 0);
pf = (GameObject)Resources.Load("CharacterRosterPanelPrefab");
pfs = (GameObject)Resources.Load("StudentPanelStudentPrefab");
for (int i = start; i < start + last; i++)
{
if (uimanager.menuID == 2 || uimanager.menuID == 3) //only populate on student or roster screen
{
// setting text components on the panels to the values in the character array
panelPos += Vector3.down * (62f); //panel offset
temp = (GameObject)Instantiate(pf, panelPos, Quaternion.identity); //temp object for panels
ThereCanBeOnlyOne(); //makes sure there's only 1 student
StudentText(); //sets student text to their values
if (characterRoster[i].isStudent && studentDraw) //if student put it in the student, if not put it in the roster
{
temp = (GameObject)Instantiate(pfs, new Vector3(0,-40,0), Quaternion.identity);
Debug.Log("student" + characterRoster[i]);
temp.transform.SetParent(studentPanel.transform, false);
student.StudentAssign(characterRoster[selectedID]);
}
if (!characterRoster[i].isStudent)
temp.transform.SetParent(rosterPanel.transform, false); //parenting non student panels under roster
}
else
break;
}
studentDraw = false;
}
There also may be something with how I'm calling this function when changing to the panel in my menu class (which I've trimmed to just the roster stuff):
public class UIManager : MonoBehaviour {
public int menuID = 0;
public GameObject[] menuPanels;
public GameObject rosterPanel;
public Roster roster;
public NewTurn nextTurn;
void Start()
{
menuPanels = GameObject.FindGameObjectsWithTag("MenuPanel");
MenuSwitch(menuID);
}
public void MenuSwitch(int menuID)
{
foreach (GameObject panel in menuPanels)
{
panel.gameObject.SetActive(false);
}
switch (menuID)
{
case 2:
rosterPanel.gameObject.SetActive(true);
break;
}
}
public void OpenRoster()
{
menuID = 2;
roster.WipeRoster();
roster.studentDraw = false;
MenuSwitch(2);
roster.PopulateRosterPanel( 0, roster.lastCharacterOnRosterScreen);
}
}
If anyone could offer any insight, I'd greatly appreciate it.
[EDIT] I realized that I could do a work around with an OnPointerDown event for the button object so I'll probably do that for now. I'm still confused as to why the buttons aren't working normally, and if anyone knows I'd still appreciate the help.
Your answer
Follow this Question
Related Questions
I cant assign a function to a button. *free candy for whoever helps me out* 2 Answers
Destroy a prefab from another class 2 Answers
Destroying Instantiated Prefab in Another Script -- Can't destroy to prevent memory loss? 0 Answers
Unity 5.3.4f1 Inconsistent Animation times between mobile devices. 0 Answers
Prefabs and public variables 1 Answer