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 /
avatar image
0
Question by Avash · Sep 19, 2014 at 01:17 PM · errorscriptingbasics

Checking null variable error

 bool empty;
 if (empty == null){
         //do something
         }

Use of unassigned local variable. This doesn't make any sense. How do I fix this?

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

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

Answer by tanoshimi · Sep 19, 2014 at 01:25 PM

What doesn't make any sense? empty is unassigned, as the error message states. You fix it by assigning a value to the variable, which can either be on the same line as the declaration, or any time before it is used.

Since bool is not a nullable type, you must either assign it the value true or false:

 bool empty = false;
 if (empty == false){
     //do something
 }

Or, if you're using a nullable type such as a string, you could assign a null value as follows:

 string empty = null;
 if (empty == null){
     //do something
 }
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 Avash · Sep 19, 2014 at 01:33 PM 0
Share

I want to assign it in if expression.

avatar image tanoshimi · Sep 19, 2014 at 01:37 PM 0
Share

But a bool can never be null, so the if statement will never be true...

avatar image Lex_87 · Sep 19, 2014 at 01:41 PM 0
Share

Ok, Avash. As Tanoshimi says, you can use nullable types. In your case it will be like this:

 bool? empty = null;
 if (empty == null){
     // do something
     // and for example:
     empty = true;
 }

avatar image
0

Answer by Lex_87 · Sep 19, 2014 at 01:36 PM

Boolean is value type in .NET FCL type library. Value types hasn't null state. In C# idiomas you have to initialize local variables, then use them in the same context. Except that cases where you guaranteed take a value through ref keyword. In this case local variable initialization will be redundant. As Tanoshimi says you can initialize first and use them safe below.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

simple animation 1 Answer

Semicolons are seen as an unexpected symbol 1 Answer

Convert type `float' to `bool', Load 1 Answer

Delay between animations 1 Answer

Error Java script 1 Answer


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