Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Jun 13, 2013 at 05:32 PM by fafase for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by AusAndrew19 · Jun 13, 2013 at 04:44 PM · javascriptif-statementsnull

Why does if(levelToLoad ==null) not work?

I'm creating a script, here is a snippet,

 var levelToLoad : String;
 
 function Start () 
 {
     if(levelToLoad ==null)
     {
         Debug.LogError("Have You Forgotten To Fill In levelToLoad?");
     }
 }

So i want the editor to tell me that i forgot to fill it in if its empty, but this does not work... There is no Debug.LogError even if its empty... Thanks for any information.

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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by fafase · Jun 13, 2013 at 05:28 PM

As opposed to the other answer you got, string can be null and should be null in C# and other language

It is a safe way to avoid crash while trying to use an non-initialized string.

 // Crash
 string str;
 Console.WriteLine(str);
 // No Crash
 string str = null;
 Console.WriteLine(str);

But a simple declaration does not set the string to null. You need to do it:

 var levelToLoad : String = null;
  
 function Start ()
 {
   if(levelToLoad ==null)
   {
     Debug.LogError("Have You Forgotten To Fill In levelToLoad?");
   }
 }

EDIT: I realized Unity does not take comparison (at least not in UnityScript) so you need to use:

 if(String.IsNullOrEmpty(levelToLoad))
Comment
Add comment · Show 6 · 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 AntiLunchBox · Jun 13, 2013 at 05:31 PM 0
Share

wups ya you're right. thinking of ints!

avatar image AusAndrew19 · Jun 13, 2013 at 06:05 PM 0
Share

Even if i add levelToLoad : String = null; this does not work.. I really appreciate you information.

avatar image fafase · Jun 14, 2013 at 06:01 AM 0
Share

Ok my bad, the simple comparison does not do it in Unity !!!??? You need to use if(String.IsNullOrEmpty(levelToLoad))

avatar image AusAndrew19 · Jun 14, 2013 at 08:47 AM 0
Share

Great, Thank you.

avatar image Bunny83 · Jun 14, 2013 at 09:10 AM 0
Share

IsNullOrEmpty is always the safest way. $$anonymous$$eep in $$anonymous$$d that Unity will initialize public variables automatically in the inspector which would set strings to an empty string and not to null.

A string is a reference type, but it's treated differently by the compiler and usually is never null since it acts more like a value type. You can set it explicitly to null, but the value you assign in field-initializers are overwritten by Unity when the object is deserialized at the beginning.

As general rule: Inspector-value > field-initializer-value.

So no matter what you set in the script, the inspector will overwrite it.

Show more comments
avatar image
1

Answer by AntiLunchBox · Jun 13, 2013 at 05:20 PM

strings aren't supposed to be null--well atleast not in c#. if c# you would do:

if(String.IsNullOrEmpty(levelToLoad))

or

if(levelToLoad == "")

one of those will work.

Comment
Add comment · Show 3 · 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 AusAndrew19 · Jun 13, 2013 at 05:22 PM 0
Share

Okay great! Successfully implemented if(levelToLoad == ""). Thank you for your time :) I also appreciate you explaining that Strings aren't supposed to be null.. Very helpful. Thank you! :)

avatar image AntiLunchBox · Jun 13, 2013 at 05:32 PM 0
Share

fafase down below is right about strings being null, its floats and ints that can't be null.

avatar image Bunny83 · Jun 14, 2013 at 09:12 AM 0
Share

Well, it's even possible to declare a nullable int or float, but yes, usual value types can't be null.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Streamwriter returning Null Javascript 1 Answer

If/else statements working one way, and not the other 0 Answers

Null values in Unity 1 Answer

not sure whats happened :S 1 Answer

Do I need to explicitly test for null or is testing the object itself good enough? 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