- Home /
If function won't work when comparing strings sent from a micro-controller.
I am using a micro-controller with Unity through the extension Ardity to send messages to Unity. I am using the micro-controller's accelerometer to determine whether the micro-controller is moving up, down or rotating left and right and then sending those words to Unity as strings. Unity is receiving the information and displaying 'up', 'down', etc in the debug console when I test it, but as soon as I compare the message to a string in an if statement, the if statement doesn't work, even though no errors are identified.
I coded the micro-controller using Arduino IDE. If you would like, I can post that code as well.
Any help would be amazing. Thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
void OnConnectionEvent(bool success)
{
if (success)
Debug.Log("Connection established");
else
Debug.Log("Connection attempt failed or disconnection detected");
}
void OnMessageArrived(string msg)
{
Debug.Log(msg); // this works
if (msg == "Right")
{
// move right
// for example, I wrote Debug.Log("Working") here and it wouldn't work
}
else if (msg == "Left")
{
// move left
}
else if (msg == "Up")
{
// move Up
}
else if (msg == "Down")
{
// move down
}
}
}
Well, for testing, what's the absolute, exact text being sent to you? You can verify that like this:
Debug.Log(msg);
for(int i = 0; i < msg.Length; i++)
{
Debug.Log(string.Format("{0}: {1}", msg[i], (int)msg[i].ToString("X2"));
}
This *should* give output that might look something like this:
Up
U: 55
p: 70
Then, if there are any anomalous/unexpected chars in the text, it should quickly become apparent.
Right, with using System.Linq;
at the top this could be done in a one liner :)
Debug.Log(msg.Aggregate("hex: ", (res, c) => res + ((int)(c)).ToString("X4") + " "));
Keep in $$anonymous$$d that a char is 16 bit. Of course we don't know where the data comes from but potentially the characters could be outside the byte range.
Whoops, right. I've been working with too many 8-bit ASCII chars lately; I've updated my comment to the safer "int" variant for consistency.
Answer by Bunny83 · Nov 22, 2021 at 11:03 PM
Try this instead:
Debug.Log(">" + msg + "< Length: " + msg.Length);
Make sure there are no spaces or any other non-printable characters like new line or carriage return in that string. You could use msg = msg.Trim();
to remove any leading or trailing whitespace characters. Though if there are other characters it would not work.
Answer by unity_KSBOMhWytx1Guw · Nov 23, 2021 at 08:20 AM
Thankyou so much. I got it working and the function is now running. I used the code above to find the exact messages being sent and found the same number on every message which turned out to be a space. This meant that the messages weren't equaling each other. I used the 'msg = msg.Trim();' code to remove the spaces and it worked perfectly.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Tips on how to handle multiple if statemens 0 Answers
Input.GetKey(KeyCode.E) requires multiple presses. 1 Answer
If statement behaves unexpectedly 3 Answers