• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Crazy Noa · Apr 11, 2014 at 12:07 PM · c#arraycustom editordialogue

Arrays with zero length

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.

alt text

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?

editor dialog.png (23.1 kB)
Comment

People who like this

0 Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GeorgesAbitbol · Apr 11, 2014 at 12:37 PM 0
Share
  • Did you try to check array.length just after the declaration of the array ?

  • Not sure if that applies in this situation, but in unityscript, if you declare a variable out of a function, like "var fubar = 1", and add that component to an object, then edit your script to change it to "var fubar = 10", the existing component keep their old "personal" values.

avatar image Crazy Noa · Apr 11, 2014 at 02:18 PM 0
Share

Yeah, I've tried checking the length in all kinds of different ways. I've also made some that mimic the ones that do work as closely as possible, but the result is still the same.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Eric5h5 · Apr 11, 2014 at 01:52 PM

Public variables take their value from the inspector, not your code. Writing "public string [] Dialogue = new string [10000];" doesn't mean there will be 10K entries in the array, if you changed it in the inspector.

Also, you should really be using a custom class instead of a bunch of separate arrays.

Comment

People who like this

0 Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Crazy Noa · Apr 11, 2014 at 02:14 PM 0
Share

I have not changed their values from the inspector after declaring them. They're hidden in the custom editor, so setting the length is impossible without going into the debug menu. It happens if I add the script to a new object as well, most arrays will have a length of 10000 while some seemingly random ones have zero entries. I'll try setting some of them to not be public though, just in case.

I'll look into making a custom class as well. I'm still new to programming and largely self taught, so most of the time I just take the easiest way out and run with it. Would that improve performance as well?

EDIT: Setting them to protected only gave me more errors, as it prevents the custom editor script from accessing them. Nothing happened to the length either, some of them are still zero.

avatar image Hoeloe · Apr 12, 2014 at 07:53 PM 0
Share

If the length is hidden in the inspector, then it's possible that Unity is still initialising them from there, but that you can't do anything about it because it's hidden. Have you tried initialising it from Start or Awake, if possible, rather than at the declaration?

Either that or setting it Non-Serializable, as previously suggested, might help.

avatar image Crazy Noa · Apr 12, 2014 at 10:08 PM 0
Share

If I initialize them from anywhere else I won't be able to store any data in them. They'll be reset each time I run the script. Wouldn't the same thing be true if I set them to non-serializable? While I have thought about storing them in some kind of database and saving/loading the data each time, I would prefer not having to work around the issue.

avatar image nikocr33nzz · Jan 02, 2017 at 03:30 PM 0
Share

@Eric5h5 : hello sir, did you know why the arrays length is always changing? i have set the size of array in the inspector to 4. but when i run the game , the size is begin from zero and if one of the index is null the size is also decrease. this is the code.

 public GameObject[] enemy = new GameObject[4]  // i also set it to 4 in the inspector
 
 void update ()
 {
      enemy = GameObject.FindGameObjectwithTag("Enemy"); 
 }

 

as you can see , i put the gameobject to the array in update. because the gameobject is not created just yet. is it because i put it in the update so the size of array is always changing? did you have another solusion for this? thanks before

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

holding GUITextures in a array? and changing the size from script? 2 Answers

How to prevent prefab of custom editor inspector from overriding array size. 1 Answer

Distribute terrain in zones 3 Answers

Trouble updating old Unity syntax 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges