teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

int message = int.Parse("123"); を int i = int.Parse("message");に変更しint i;を記述しました。

2021/01/03 06:58

投稿

NATU72
NATU72

スコア1

title CHANGED
@@ -1,1 +1,1 @@
1
- UnityでOperator '==' cannot be applied to operands of type 'string' and 'int'というエラーが発生
1
+ UnityでFormatException: Input string was not in a correct format.というエラーが発生
body CHANGED
@@ -4,7 +4,7 @@
4
4
  Arduino側ではボタンを押すとシリアルモニタに"1"と表示され、押していない時は"0"と表示されるのでUnity側で1という数字を受け取るとonClick.Invoke();が実行されるという流れにしたいです。
5
5
 
6
6
  ### 発生している問題・エラーメッセージ
7
- error CS0019: Operator '==' cannot be applied to operands of type 'string' and 'int'
7
+ FormatException: Input string was not in a correct format.
8
8
 
9
9
  ### 該当のソースコード
10
10
 
@@ -17,12 +17,13 @@
17
17
  public class SerialButton : MonoBehaviour {
18
18
  public SerialHandler serialHandler;
19
19
  public Button itemBox;
20
+ int i;
20
21
 
21
22
  // Use this for initialization
22
23
  void Start () {
23
24
  //信号を受信したときに、そのメッセージの処理を行う
24
25
  serialHandler.OnDataReceived += OnDataReceived;
25
- int message = int.Parse("123");
26
+ int i = int.Parse("message");
26
27
  }
27
28
 
28
29
  // Update is called once per frame
@@ -35,15 +36,12 @@
35
36
  */
36
37
  void OnDataReceived(string message) {
37
38
  try {
38
- if(message == 1){
39
+ if(i == 1)
40
+ {
39
41
  itemBox.onClick.Invoke();
40
42
  }
41
43
  } catch (System.Exception e) {
42
44
  Debug.LogWarning(e.Message);
43
45
  }
44
46
  }
45
- }
47
+ }
46
-
47
- ### 試したこと
48
-
49
- 文字列であるmessageを数字に変換するために記述したint.Parse("123");のソースコード内の別の場所に記述してみましたが変わりませんでした。