Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by IHackedDeath · Dec 14, 2015 at 02:13 AM · textnullif statement

null text check should return null but apparently isn't null

Hello all,

As my question states I have a text check in an if statement to see if it is null but it comes back apparently not null when I clearly know there is no text in the text box.

Here is my code:

     GameObject LobbyWindow;
 
     void Start ()
     {
         LobbyWindow = GameObject.FindGameObjectWithTag ("Lobby");
         LobbyWindow.SetActive (false);
     }
 
 public void StartServer()
     {
         Network.InitializeServer(16, 7777, useNat);
         createLobby ();
     }
 
 void createLobby()
     {
         LobbyWindow.SetActive (true);
         if (LobbyWindow.GetComponentInChildren<Text> ().text == null) 
         {
             Text newtext = LobbyWindow.GetComponentInChildren<Text> ();
             newtext.text = this.userName.text;
             Debug.Log ("this is working");
             newtext = this.userName;
         }
     }

The LobbyWindow GameObject is a Panel and I have 8 empty text box's in the Panel.

Any and all help is appreciated.

Kind Regards,

IHackedDeath.

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

1 Reply

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

Answer by maccabbe · Dec 14, 2015 at 03:06 AM

No text can mean that the string either empty (=="") or null (==null). You can either check for both using

 if (text == null || text == "")

or use the convenience method String.IsNullOrEmpty

 if (String.IsNullOrEmpty(text))

https://msdn.microsoft.com/en-us/library/system.string.isnullorempty(v=vs.110).aspx

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 IHackedDeath · Dec 14, 2015 at 03:20 AM 0
Share

Um, ok.

I don't understand, why wouldn't it have both as the value so you could go either way but not have to check for both, that is very annoying and a nuisance.

Will give this a try and see how it goes, thank you maccabbe.

avatar image Bunny83 IHackedDeath · Dec 14, 2015 at 07:15 AM 3
Share

A string is a reference type and is allocated on the heap. A string, like any object, can be null. So there's actually no string object allocated, it's just a null reference. An actual string object can also represent an empty string. So there's still an object allocated and the variable holds a reference to that string object, but the string itself has zero length.

Empty strings are quite common and useful. For example:

 string s = null;
 Debug.Log(s.Length); // This will throw a null reference exception. You can't read the length of the string since the variable doesn't reference a string object.
 s += "Hello World!";  // This also doesn't work since you can't concat null with a string.

With an empty string that's all possible:

 string s = "";
 Debug.Log(s.Length); // this will print "0"
 s += "Hello";
 s += " World!"; // now "s" contains "Hello World!".

Ins$$anonymous$$d of "" you can also use string.Empty which is actually used by the compiler internally. See Empty for more details.

ps: This behaviour is nothing special in C#. It almost always works in a similar way in other languages.

avatar image dmpitu Bunny83 · Oct 30, 2017 at 02:03 PM 0
Share

Thank you very much! Your theoretical explanation clarified my doubts about the "error reference" I was having. I solved it by changing the declaration of the variable "string myvariable;" by "string myvariable =" "; Really thanks you

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

31 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 avatar image avatar image avatar image avatar image

Related Questions

Use of if(Component) 1 Answer

if (Something != null) not good enough 2 Answers

How To Load TTF Font From External File 0 Answers

How to link up instantiated text in list to allow buttons to adjust number shown, c# 1 Answer

Adventure Game Help 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