Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.81 KB | None | 0 0
  1. Hierarchy:
  2. Canvas
  3.  -> AssignOfficerMenu (containing script AssigningOfficers)
  4.     -> AssignOfficerPanel (instantiated prefab with both children, no scripts)
  5.        -> OfficerPanel (displays info, no scripts)
  6.        -> Dropdown (containing script AssignignOfficerDropdown)
  7.  
  8. Quick summary:
  9. AssignOfficerMenu shows panels of data corresponding to officer. There is dropdown with each panel so you can choose what officer should be doing.
  10. 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.
  11.  
  12. Problem:
  13. Trying to do "Dropdown.value = someInt" crashes Unity with no error log of any kind.
  14.  
  15. ----------------AssigningOfficerDropdown script:-------------------
  16. public class AssigningOfficerDropdown : MonoBehaviour {
  17.    public Dropdown myDropdown;
  18.    public PlayerData DataSource;
  19.    public AssigningOfficers AssignOfficerMenu;
  20.    public int ThisOfficer;
  21.  
  22.    void Awake() {
  23.        myDropdown = this.GetComponent<Dropdown>();
  24.        DataSource = GameObject.Find("GlobalController").GetComponent<PlayerData>();
  25.        AssignOfficerMenu = this.transform.parent.parent.GetComponent<AssigningOfficers>();
  26.        myDropdown.onValueChanged.AddListener(delegate {
  27.            AssignOfficerMenu.UpdateOfficer(ThisOfficer);
  28.        });
  29.        myDropdown.onValueChanged.AddListener(delegate {
  30.            AssignOfficerMenu.ChosenOption(myDropdown);
  31.        });
  32.    }
  33.  
  34.    void Destroy() {
  35.        myDropdown.onValueChanged.RemoveAllListeners();
  36.    }
  37. }
  38. ----------------AssigningOfficers script:-------------------
  39. public class AssigningOfficers : MonoBehaviour {
  40.    public PlayerData DataSource;
  41.    public GameObject AssignOfficerPanel;
  42.    private bool OfficerChanged = false;
  43.    private int ChangedId = 0;
  44.  
  45.    void Start () {
  46.        DataSource = GameObject.Find("GlobalController").GetComponent<PlayerData>();
  47.        UpdateOfficersList();
  48.    }
  49.  
  50.    public void UpdateOfficersList() {
  51.        if (this.transform.childCount != 0) {
  52.            List<GameObject> tempChild = new List<GameObject>();
  53.            foreach (Transform child in this.transform) {
  54.                tempChild.Add(child.gameObject);
  55.            }
  56.            foreach (GameObject tempObj in tempChild) {
  57.                Destroy(tempObj);
  58.            }
  59.        }
  60.  
  61.        foreach (Officer officer in DataSource.OfficerList) {
  62.            GameObject tempObj = Instantiate(AssignOfficerPanel, this.transform);
  63.            Dropdown tempDrop = tempObj.transform.FindChild("Dropdown").GetComponent<Dropdown>();
  64.            Text tempClass = tempObj.transform.FindChild("OfficerPanel").FindChild("Class").GetComponent<Text>();
  65.            Text tempJob = tempObj.transform.FindChild("OfficerPanel").FindChild("Job").GetComponent<Text>();
  66.  
  67.            tempDrop.AddOptions(GlobalData.OfficerStatusNames);
  68.            tempDrop.transform.GetComponent<AssigningOfficerDropdown>().ThisOfficer = officer.Id;
  69.            try {
  70.                tempDrop.value = (int)officer.Status;
  71.            } catch (UnityException ex) {
  72.                System.Diagnostics.Debug.WriteLine(ex);
  73.                Debug.Log(ex);
  74.            }
  75.            tempJob.text = officer.Status.ToString();
  76.            tempClass.text = officer.Class;
  77.        }
  78.    }
  79.  
  80.    public void UpdateOfficer(int id) {
  81.        OfficerChanged = true;
  82.        ChangedId = id;
  83.    }
  84.  
  85.    public void ChosenOption(Dropdown dropdown) {
  86.        if (OfficerChanged) {
  87.            int index = DataSource.OfficerList.FindIndex(x => x.Id == ChangedId);
  88.            DataSource.OfficerList[index].Status = (OfficerStatus)dropdown.value;
  89.            OfficerChanged = false;
  90.            UpdateOfficersList();
  91.        }
  92.    }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement