0

I have a simple piece of code I run in Unity:

string cmd = inputField.text;

foreach (var command in _commands)
{
    Debug.Log("." + command.commandPattern + ".");
    Debug.Log("." + cmd + ".");
    Debug.Log(String.Equals(command.commandPattern.ToString(), cmd.ToString()));

}

_commands is a list of Command objects and it contains one ResetCommand which inherits from Command. Here are the classes:

[Serializable]
public class Command
{
    [SerializeField]
    public string commandPattern;

    public void Execute() { }
}

[Serializable]
public class ResetCommand : Command
{
    public ResetCommand()
    {
        commandPattern = "reset";
    }

    public new void Execute()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
}

After running the code the Unity Console output looks like this:

Unity Console

The comparison doesn't seem to work. Any idea what I'm doing wrong here?

EDIT: Here is a screenshot of a debugger.

scr

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Wojtek Wencel
  • 2,257
  • 6
  • 31
  • 65
  • 2
    Have you tried printing out the ascii/hex code and compare these? `Debug.Log(BitConverter.ToString(Encoding.UTF8.GetBytes(cmd));` – derHugo Jan 13 '21 at 10:36
  • 2
    And why do you `ToString` if these are already strings? – derHugo Jan 13 '21 at 10:38
  • @derHugo you are right, the `cmd` variable has extra `E2-80-8B` at the end. Ant idea why it's there and how to get rid of that? – Wojtek Wencel Jan 13 '21 at 10:40
  • 1
    [Simplest way to get rid of zero-width-space in c# string](https://stackoverflow.com/questions/24942167/simplest-way-to-get-rid-of-zero-width-space-in-c-sharp-string) – derHugo Jan 13 '21 at 10:48
  • @derHugo Thanks, it works! If you want to add an answer for me to accept then go ahead. – Wojtek Wencel Jan 13 '21 at 10:52

0 Answers0