Search Unity

Odd C# char problem

Discussion in 'Scripting' started by Drowning-Monkeys, Jan 24, 2019.

  1. Drowning-Monkeys

    Drowning-Monkeys

    Joined:
    Mar 6, 2013
    Posts:
    302
    Hi there,

    So this is crazy...
    I have a list of char's i want to ignore from some strings im working with (It's a word game):

    Code (CSharp):
    1. public List<char> StaticCharacters = new List<char> { ',', '.', '\'', '&', '$', '#', '@', '\"', '-', '!', ' ' };
    But when the game compiles, it removes the hyphen and the exclamation point from the list. The list literally goes from a count of 11 to a count of 9.

    Now I realize i can just do this:
    Code (CSharp):
    1. bool myBool = char.IsPunctuation(testChar);
    2.  
    But that's not the point of this list, as it's to limit specific characters I want, and in any case i thought this very interesting.

    Any ideas? I've also tried (char)45 for the hyphen for example, no dice.
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    709
    How did you find out about this behavior?
    Actually, as long as you do not change the content of the list somewhere in your code, this should not happen. Probably the problem lies somewhere else.

    To make sure that the list is not changed, use this code instead:
    Code (CSharp):
    1. public static readonly ReadOnlyCollection<char> StaticCharacters = (new List<char> { ',', '.', '\'', '&', '$', '#', '@', '\"', '-', '!', ' ' }).AsReadOnly();
     
    Brathnann likes this.
  3. Drowning-Monkeys

    Drowning-Monkeys

    Joined:
    Mar 6, 2013
    Posts:
    302
    Thanks for the reply.

    So this is interesting - we are not touching or altering the list. In fact the list is referenced at only 1 line of code:
    Code (CSharp):
    1. IsStatic = StaticCharacters.Contains(InChar);
    I duplicated the list, and then did a 3rd list with the parameters you mentioned and neither lost their data, so.......... very very very weird.
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,532
    Is that field a member of a MonoBehaviour or any other type that's marked serializable?

    Also, the name suggests the list is supposed to be static, but your code does not declare it as static. So if the former applies, the latter would cause the list to be serialized.

    If that's the case, it's worth to note that already serialized instances will not be updated when you change field initalizers, instead they keep their latest, serialized values.

    Perhaps that's what you've observed?
     
    Antistone and Owen-Reynolds like this.
  5. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    857
    Or the short version: is that script on a gameObject? If so, look at the list in the Inspector.
     
    Suddoha likes this.
  6. Drowning-Monkeys

    Drowning-Monkeys

    Joined:
    Mar 6, 2013
    Posts:
    302
    I'll keep hunting. thanks for the help. went ahead and changed it to static readonly