Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Hierarchy:
- Canvas
- -> AssignOfficerMenu (containing script AssigningOfficers)
- -> AssignOfficerPanel (instantiated prefab with both children, no scripts)
- -> OfficerPanel (displays info, no scripts)
- -> Dropdown (containing script AssignignOfficerDropdown)
- Quick summary:
- AssignOfficerMenu shows panels of data corresponding to officer. There is dropdown with each panel so you can choose what officer should be doing.
- I'm using two delegates on Dropdown valueChanged because I moved script to the parent element, because at first I thought it is crashing because I was destroying object which initialized it at first place but as you see, it was not the case.
- Problem:
- Trying to do "Dropdown.value = someInt" crashes Unity with no error log of any kind.
- ----------------AssigningOfficerDropdown script:-------------------
- public class AssigningOfficerDropdown : MonoBehaviour {
- public Dropdown myDropdown;
- public PlayerData DataSource;
- public AssigningOfficers AssignOfficerMenu;
- public int ThisOfficer;
- void Awake() {
- myDropdown = this.GetComponent<Dropdown>();
- DataSource = GameObject.Find("GlobalController").GetComponent<PlayerData>();
- AssignOfficerMenu = this.transform.parent.parent.GetComponent<AssigningOfficers>();
- myDropdown.onValueChanged.AddListener(delegate {
- AssignOfficerMenu.UpdateOfficer(ThisOfficer);
- });
- myDropdown.onValueChanged.AddListener(delegate {
- AssignOfficerMenu.ChosenOption(myDropdown);
- });
- }
- void Destroy() {
- myDropdown.onValueChanged.RemoveAllListeners();
- }
- }
- ----------------AssigningOfficers script:-------------------
- public class AssigningOfficers : MonoBehaviour {
- public PlayerData DataSource;
- public GameObject AssignOfficerPanel;
- private bool OfficerChanged = false;
- private int ChangedId = 0;
- void Start () {
- DataSource = GameObject.Find("GlobalController").GetComponent<PlayerData>();
- UpdateOfficersList();
- }
- public void UpdateOfficersList() {
- if (this.transform.childCount != 0) {
- List<GameObject> tempChild = new List<GameObject>();
- foreach (Transform child in this.transform) {
- tempChild.Add(child.gameObject);
- }
- foreach (GameObject tempObj in tempChild) {
- Destroy(tempObj);
- }
- }
- foreach (Officer officer in DataSource.OfficerList) {
- GameObject tempObj = Instantiate(AssignOfficerPanel, this.transform);
- Dropdown tempDrop = tempObj.transform.FindChild("Dropdown").GetComponent<Dropdown>();
- Text tempClass = tempObj.transform.FindChild("OfficerPanel").FindChild("Class").GetComponent<Text>();
- Text tempJob = tempObj.transform.FindChild("OfficerPanel").FindChild("Job").GetComponent<Text>();
- tempDrop.AddOptions(GlobalData.OfficerStatusNames);
- tempDrop.transform.GetComponent<AssigningOfficerDropdown>().ThisOfficer = officer.Id;
- try {
- tempDrop.value = (int)officer.Status;
- } catch (UnityException ex) {
- System.Diagnostics.Debug.WriteLine(ex);
- Debug.Log(ex);
- }
- tempJob.text = officer.Status.ToString();
- tempClass.text = officer.Class;
- }
- }
- public void UpdateOfficer(int id) {
- OfficerChanged = true;
- ChangedId = id;
- }
- public void ChosenOption(Dropdown dropdown) {
- if (OfficerChanged) {
- int index = DataSource.OfficerList.FindIndex(x => x.Id == ChangedId);
- DataSource.OfficerList[index].Status = (OfficerStatus)dropdown.value;
- OfficerChanged = false;
- UpdateOfficersList();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement