Hello, I have created a DLL which contains my logic. I could import the DLL successfully. But when i call the LoadData method from my Editorscript Unity crashes when the LoadData method returns without an error or something. In the Editor Log i can see the Debugs are all called. So i think when LoadData returns there is some error but i cant find anything. LoadData is a function from my external DLL. Of course all my functions are in classes. I just copied the important parts. Code (CSharp): using UnityEngine; using System.Xml.Serialization; using System.Collections.Generic; using System.IO; using System.Text; using System.Xml; // From External DLL public static Language LoadData(string filePath) { try { // When File exist if (File.Exists(filePath)) { // create a variable to store the result Language result; // create a TextReader StreamReader textReader = new StreamReader(filePath, true); // create a XmlReader XmlReader xmlReader = XmlReader.Create(textReader); // create a XmlSerializer XmlSerializer xmlSerializer = new XmlSerializer(typeof(Language)); Debug.Log("Vorbereitung"); // When xmlSerializer can Deserialize xmlReader if (xmlSerializer.CanDeserialize(xmlReader)) { Debug.Log("Deserialisieren"); // store the object as result result = xmlSerializer.Deserialize(xmlReader) as Language; Debug.Log("fertig Deserialisiert"); // close the textreader textReader.Close(); Debug.Log("Return"); // return object return result; } } // return default object return null; } catch(UnityException ex) { Debug.LogException(ex); } return null; } Code (CSharp): using UnityEngine; using UnityEditor; using System.IO; using LocalizationData; using System.Collections.Generic; private void OnGUI() { GUIIsFileSelected(); } private void GUIIsFileSelected() { // When no File is selected if (!bFileSelected) { // Show a Load File Button if (GUILayout.Button("Load File")) { // When the button is clicked open a select file dialog string filePath = SelectFile(); Debug.Log("File path: " + filePath); // When string is not null or empty if (!string.IsNullOrEmpty(filePath)) { Debug.Log("not null or empty"); SelectedLanguage = Language.LoadData(filePath); Debug.Log("Set file selected"); SetFileSelected(true); } // When string is null or empty else { Debug.LogWarning("Selected File is not valid."); } } return; } }
In this moment I found the error. It was the declaration of my property. Can someone explain it to me? Code (CSharp): // This work's fine public Language SelectedLanguage { get; set; } // This didn't work public Language SelectedLanguage { get { return SelectedLanguage; } set { SelectedLanguage = value; } }
Endless recursion (or stack overflow - depends on the situation) . The getter is defined in such a way that is calls itself. Setter calls itself as well. You either just declare get;set; if no further implementation detail is needed so far, or you declare the backing field(s) which are supposed to be accessed by the property.
if you're using the the first section is what's called an auto-implemented property. in your "This didn't work" section, you've told the compiler you're taking over implementation of the property. then you tell it to return itself. that's the recursion suddoha is talking about. Change it to use a separate variable like _selectedLanguage for both the getter and setter. or just use the first one.