Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Null? How to check if a string table has nothing

Discussion in 'Scripting' started by Findo, May 3, 2014.

  1. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
    I know null = ""

    but what is nothing?



    IF I try to type if null when it isn't there, it gives me this error.


    So yeah, I'm frustarated :(

    I did like.. if TheTable'sName[4] == null
    And it seems to not be working D:
     
  2. Smooth-P

    Smooth-P

    Joined:
    Sep 15, 2012
    Posts:
    214
    Trying to index and element outside the range of the list / array is an error, not null.

    This situations seems like you should rethink your code, but for now you can try:

    Code (csharp):
    1.  
    2. var i = 4;
    3.  
    4. if (list.Count <= i || list[i] == null) {
    5.      // foo
    6. }
    7.  
     
  3. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Null is not ""

    Null is nothing.

    "" is an empty string.
     
  4. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
    DanielQuick, tell that to my errors -_- AND to my empty string that counts as null in an if statement.
     
  5. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Your error is due to trying to access something that isn't there. You should never access an element in a List with an index greater than or equal to the number of elements in the list.

    A string can be null or it can be empty. It can't be both.