Q&A
###前提・実現したいこと
①Unityにおいて、InputFieldで得た数値(入力1と入力2)の合計値を算出
②計算結果をuGUIのTextコンポーネントに表示させたい
###発生している問題・エラーメッセージ
超絶初心者で申し訳ございません。JavaやC#を1年ほど週末趣味でやってきて、ようやくUnityに挑戦するぞ、となったものの、いきなり躓いております。
そもそも合計値を算出できているか否かすら不明です。Debug.Logでも出てきません。
uGUIで出したTextコンポーネントに計算結果を代入して出力する方法を数日書けて調べたのですが、いくら調べてもわかりません。
(Textコンポーネントはその場でスクリプトで生成するものではなく、予め設置してあるものを使用したい)
エラーメッセージ
UnityException: You are not allowed to call this function when declaring a variable.
Move it to the line after without a variable declaration.
If you are using C# don't use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function.
Calc..ctor ()
Find can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
UnityEngine.GameObject:Find(String)
Calc:.ctor()
ArgumentException: Find can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
Calc..ctor ()
###該当のソースコード
C#(Calc.cs)
using UnityEngine;
using System;
using UnityEngine.UI;
public class Calc : MonoBehaviour
{
public int total; //合計金額
//入力されたテキストを受け取ってコンソールにログを吐き出す public void ValueChange(string text) { Debug.Log(text); } //InputFieldに入力されたものを受け取る。受け取る時の形式はテキスト。 Text textInput1 = GameObject.Find("FieldInput1_Text").GetComponent<Text>(); Text textInput2 = GameObject.Find("FieldInput2_Text").GetComponent<Text>(); //テキストを数値に変換 public int input1 = int.Parse("FieldInput1_Text"); public int input2 = int.Parse("FieldInput2_Text"); //合計値を算出 public void CalcTotal() { int total = input1 + input2; Debug.Log(total); }
}
###試したこと
Textボックスではなく、InputFieldに出力してみようともしてみたのですが、ダメでした。
###補足情報(言語/FW/ツール等のバージョンなど)
Unity5.3.5f1
InputFieldで入力されたTextを取り込む方法は主にこちらを参考にしました。
しかし、出力する方法が一向に分かりません。。。
http://www.peliphilo.net/archives/1040
TextFieldをスクリプトで生成して表示させる方法は、最終的な出力のイメージが異なっております。また、足し算の結果は相変わらず出力できませんでした。
http://techacademy.jp/magazine/4198
何卒、よろしくお願い申し上げます。
###修正済み完成コード
using UnityEngine;
using System;
using UnityEngine.UI;
public class Calc : MonoBehaviour
{
//★合計金額表示用のText用変数
public Text TotalText;
//InputFieldに入力されたものを受け取る。受け取る時の形式はInputField InputField textInput1; InputField textInput2; void Start() { //Textコンポーネントの取得は最初の一度だけでいいので、Start内で行う textInput1 = GameObject.Find("FieldInput1").GetComponent<InputField>();//「("FieldInput1_Text")」ではなく「("FieldInput1")」。FieldInput1_TextオブジェクトにはInputFieldが付いていない。「("Canvas/FieldInput1")」でもOK。 textInput2 = GameObject.Find("FieldInput2").GetComponent<InputField>(); //★TotalTextに出力するにはvoid Start(){}内でGameObjectを宣言しておく必要がある。これがあれば、一番下の「TotalText.text = Total.ToString();」がNull Reference Exceptionにならない。 //★詳しくは「http://docs.unity3d.com/ja/current/Manual/ControllingGameObjectsComponents.html」を参照 TotalText = GameObject.Find("TotalText").GetComponent< Text > (); } //入力されたテキストを受け取ってコンソールにログを吐き出す public void ValueChange(string text) { //★HierarchyのFieldInput1にCalc.csをAddComponentし、On Value Changedを+し、FieldInput1をドラッグ&ドロップ1し、プルダウンでCalc>ValueChangeを選択 //★On End Editも+し、FieldInput1をドラッグ&ドロップ1し、プルダウンでCalc>CalcTotal()を選択 //★HierarchyのFieldInput2にCalc.csをAddComponentし、On Value Changedを+し、FieldInput2をドラッグ&ドロップ1し、プルダウンでCalc>ValueChangeを選択 //★On End Editも+し、FieldInput2をドラッグ&ドロップ1し、プルダウンでCalc>CalcTotal()を選択 Debug.Log(textInput1); //textInput1を受け取れているか否かの確認 Debug.Log(textInput2); //textInput2を受け取れているか否かの確認 } //合計値を算出 public void CalcTotal() { //テキストを数値に変換 int input1 = int.Parse(textInput1.text); int input2 = int.Parse(textInput2.text); //int型+int型=int型です、ValueChangeのデバッグログと見比べてみてください int Total = input1 + input2; //Totalはローカル変数 Debug.Log(Total); //計算結果をとりあえずFieldInput1_Textに戻します //.textはstring型なので、int型をそのまま入れるとエラーを起こします //int型の変数の後ろに.ToString()を付けるとstring型に変換されます(←ここポイント) //.ToString()の代わりに「+ ""」でもstring型になります //textInput1.text = total.ToString(); //★計算結果をTextコンポーネントに出力するために変更 //合計金額を出力するTextコンポーネント「TotalOutput」にCalc.csをAddComponent //InspectorのTotalTextにHierarchyからTotalOutputをドラッグ&ドロップ TotalText.text = Total.ToString();//intのTotalをToStringでString化して、TotalTextに代入 }
}
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2016/08/14 17:01 編集
2016/08/14 17:20 編集
2016/08/15 00:09
2016/08/15 10:19
2016/08/15 10:58
2016/08/16 16:18
2016/08/17 16:08
2016/08/18 14:25
2016/08/18 16:20