If Statement not working

This script is used to read a serial gpio port . The serial commands are working fine.
The text in my string show up on the console which I have ion my canvas.
When running the script bypasses the if statements and lights the yellow (image)led.
I have tried many things from manual and forums and can’t get it to work.
I have the public items attached and the button attached and reporting the string in the console.
Any ideas are appreciated.

5075699–499106–TestIn.cs (575 Bytes)

Next time post a small script like this directly into the forum using CODE tags.

I don’t understand why you’re calling ToString() on a string. But your issue most likely is that your “if” statements are failing because “cable” doesn’t exactly match the string you are comparing it to. You probably have extra white space or some other minor difference you’re not noticing from just console output. I’d try using Trim() or Contains first, if that doesn’t work try using a regex.

If that still doesn’t work, I’d output the value of cable to a text file and take a closer look outside of the editor.

Thank you for your fast reply.
I changed the debuglog to:
Debug.Log ("Length : " +cable.Length); and found the length does not match my string.
I tried Trim() but realized it only deals with first and last character.
I agree that the problem is with the "cable"string.
I am using a button to read if a serial gpio port is open and only need a 1 or 0 reported to my text object.
The calling to string line is needed as I had an error with text to string.

I am looking at modifying the line in another script that puts the text into the "Last Value text object.
The Lines from the serial value script is:
public int Column = 0;

void OnSerialValues(string[ ] values) {
if (Column < values.Length) {
GetComponent().text = “Last value [” + Column + "]: " + values[Column];

Column is a public integer if you use more than 1 text object.
I will reach out to the author of the serial asset if I can’t figure it out.
If there are any suggestions on the above script please respond.
I will gladly share my results when I have things working.
Thanks again

I modified the serial command to shorten the string and used the Trim() and
got it to work. Your advice led me in the right direction.
Thanks

1 Like

Glad you got it working!