I’m working on a system for branching dialogue. It’s quite simple. There are two variables that keep track of the current point in time and which timeline you are on. Think of them as y and x, with each point on the grid being one line of dialogue. These lines are stored in an array of length 10000. Since two dimensional arrays won’t persist I use one dimensional ones, the length being x times y. The index is determined with a simple formula (x*100+y), which gives each point on the grid its own space in the array. In addition to the dialogue array I have a bunch of other ones for settings, portrait images, sound effects etc. They work the exact same way. It’s like I have a bunch of grids layered on top of each other, each one with it’s own datatype. The x and y variables determine the current position and finds all current events. Each one of these arrays is declared at the top of the script, as public arrays of differing datatypes.
public string [] Dialogue = new string [10000];
public float [] DialogueTime = new float[10000];
public string [] ChoiceString1, ChoiceString2, ChoiceString3, ChoiceString4 = new string [10000];
public int [] NumberOfChoices = new int [10000];
public string [] Animations = new string[10000];
public MonoBehaviour[] Script = new MonoBehaviour [10000];
public Texture [] Portrait = new Texture [10000];
public AudioClip [] SoundEffect = new AudioClip [10000];
public bool [] PlayerSpeaking = new bool[10000];
This is all displayed in a custom editor.
[25060-editor+dialog.png|25060]
While this may not be a very efficient solution, it works fine for the small project I intend to use it for.
However, a few months ago I ran into a very strange problem. I had ten arrays declared from script. I used a smaller grid at the time, so each one had a length of 100. To add a new feature I needed to declare one more. When I did this and tried to use it, Unity would say that my index was out of range. After checking with array.length and experimenting for a while I discovered that the new array had a length of zero, even though it was declared and used the same way as all the other ones. There was literally nothing different except for it being the last one added.
At the time I couldn’t solve it, so I decided to try to work around it instead. But a few weeks later I desperatley needed to add one again. This time I discovered that I could go into the debug menu and set the length from there. If I did this once it would work just like expected. But having to do that each time kind of defeats the purpose of a simple, codeless custom editor. And I still have no idea what causes specifically the eleventh array to break every single time, regardless of length or datatype.
Since then I’ve been using the debug workaround. I increased the length of all arrays to 10000, upgrading the grid to 100*100. While I noticed Unity slow down from the increased workload the strange behaviour of the eleventh array is exactly the same. It does not seem to be related to memory usage at all. I’ve tried setting all arrays to only having one element and I’ve tried 10000. There is no difference whatsoever. The only constant is that the eleventh array always breaks.
I have absolutely no idea what is causing this. Performance doesn’t seem to be an issue. There is no difference in how I use them either, and I haven’t done anything to limit the number of arrays I can declare. For some bizarre reason it just stops working. I’m starting to think it’s a problem with Unity rather than me.
Does anyone have any idea of what the reason for this could be?