Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
0
Question by NoSoup4you · Sep 10, 2013 at 11:08 AM · stringnulldefaultemptyinitialize

Default string value... empty? (C#)

I've declared a public array of strings in my class without assigning values to them. I intend to enter them for each object in Unity's inspector pane. I won't always use the full array (some objects will have less options associated with them,) so I loop through it, and when it finds the first null element in the array it sets int usableLength to that index and stops looping.

Strangely, I found that usableLength always remained at 0. But if I use if (array[x].Equals("")), or IsNullOrEmpty for that matter, it works. I thought C# was supposed to initialize all types to null by default? It's not preventing me from making progress now that I've figured it out, but I'm curious as to why.

Comment
Add comment · Show 5
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 DannyB · Sep 10, 2013 at 11:12 AM 1
Share

If these arrays are filled in the inspector, why do you end up having empty values in the first place? Adjust the array length in the inspector and fill all items, not easier and safer?

avatar image NoSoup4you · Sep 10, 2013 at 11:19 AM 0
Share

I hadn't known you could change the array size in the inspector until I actually dragged my script onto an object. It could be argued that I have to do slightly less work for each object this way, but you're pretty much right... Still, I want to know about this.

avatar image ArkaneX · Sep 10, 2013 at 11:37 AM 1
Share

I guess this is just a matter of Unity developers decision. $$anonymous$$aybe they decided empty strings are more intuitive?

If you have a variable available in inspector, then you probably want to fill it with some real value. For me, empty string is such a value, while null is not.

avatar image DannyB · Sep 10, 2013 at 11:43 AM 0
Share

I guess it was a decision to allow easy work with the inspector. Otherwise, you would have needed another control near that area to say "null". In any case, good or bad, it can be easily worked around.

Also, I guess you know that you can expose arrays of your own objects in the inspector, or an array of enums, so all in all I think there are enough ways to solve each use case.

avatar image NoSoup4you · Sep 10, 2013 at 11:58 AM 0
Share

Good point. Thanks guys.

Come to think of it, by not entering anything in the inspector, it's probably "filling" the array elements with nothing.

2 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by DannyB · Sep 10, 2013 at 11:26 AM

I believe this is what is going on:

Uninitialized strings in C# will have a null default value.
Uninitialized public strings in Unity (or rather, in a MonoBehavior) will have an empty value.

This demonstrates:

 string myString;
 string[] myStrings;
 
 public string myPublic;
 public string[] myPublics;

 void Start() {
     myStrings = new string[2];
     Debug.Log(myString);     // null
     Debug.Log(myStrings[0]); // null
     Debug.Log(myPublic);     // ""
     Debug.Log(myPublics[0]); // ""
 }


And, as discussed in the comments, I would try to avoid having arrays with "holes". You can either use the inspector array size, or if it does not need to be inspector-exposed, use a generic List instead.

Finally - if you still prefer (or must) keep an array with holes, then you already found your answer. It would be item == "" if its a public inspector array, or item == null and in any case, you better off using IsNullOrEmpty.

Comment
Add comment · Show 1 · 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 TruffelsAndOranges · Sep 28, 2015 at 10:21 AM 1
Share

Wow that's interesting. I had a bug that was an implication of Unity setting public strings to another default value than the one I had given it. Thank you for spotting it for me!

avatar image
0

Answer by bezier · Jul 28, 2021 at 03:08 AM

Monobehaviour's public fields will have serialization once they get involved in some prefabs. That's why they got objects assigned when you check them in Editor or Player.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

String in array is null when checking length, help bug?! 2 Answers

Public variables null after assigned in the inspector. 3 Answers

Assigning Texture2D to script 1 Answer

Setting a variable to nothing 1 Answer

How to initialize non-static field damage into string? 2 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