###前提・実現したいこと
メインカメラにアタッチするスクリプトで、ボタンを押したらx座標が更新されてx座標が300を超えたら0に戻り、0に戻った時に対象のオブジェクト(今回は球体)の画像を張り替えるスクリプトを組もうと思っているのですが、とりあえずで組んだswitch文すら動かなくて困っています。↓の二つのエラーを検索してみてもいまいちよくわからず…
それと張り替える画像が数百枚単位になってしまうためいちいちcase1、case2と書いていくのはかなり手間がかかりそうなのですが何かいい方法はないでしょうか?
参考にしたサイトです
http://qiita.com/ntkgcj/items/61faa07e5a24c59fa950
###発生している問題・エラーメッセージ
FindGameObjectsWithTag 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:FindGameObjectsWithTag(String) mvcam:.ctor() ArgumentException: FindGameObjectsWithTag 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. mvcam..ctor ()
###該当のソースコード
using UnityEngine; using System.Collections; public class mvcam : MonoBehaviour { public Vector3 pos = new Vector3(-11, 0, 0); const string BASE_TEXTURE = "Textures/gazou"; //テクスチャを貼りたいゲームオブジェクトの配列生成(タグ名は「SphereObject」) GameObject[] spriteObjects = GameObject.FindGameObjectsWithTag("SphereObject"); //スプライト画像の配列生成(テクスチャ画像の読み込み) Sprite[] sprites = Resources.LoadAll<Sprite>(BASE_TEXTURE); // Use this for initialization void Start() { pos = transform.position; //位置保持 } void Update() { if (Input.GetKeyDown(KeyCode.Space) && pos.x < 300) { //spaceキーの入力があり、かつx座標が300以下の場合にという条件文 pos.x += 30; //x座標を+30する(移動する) transform.position = pos; //位置更新 print(transform.position.x); //コンソールを開くと座標が今どこにいるのかわかるのか書いてくれる } else if (Input.GetKeyDown(KeyCode.Space) && pos.x >= 300) { pos.x = 0; transform.position = pos; print(transform.position.x); foreach (GameObject sp in spriteObjects) { //ゲームオブジェクトのスプライトレンダラ設定を取得 SpriteRenderer spObRender = sp.GetComponent<SpriteRenderer>(); //別スクリプトの変数を取得 //(スクリプト名は「SpriteObjectNumber」、変数名は「spObNum」) int textureUniqueNumber = sp.GetComponent<SpriteObjectNumber>().spObNum; //switch文で、textureUniqueNumber の数字によって処理(貼るテクスチャ)を変える switch (textureUniqueNumber) { case 1: spObRender.sprite = sprites[0]; break; case 2: spObRender.sprite = sprites[1]; break; } } } } } 張り替える対象オブジェクトに付けるスクリプト↓ using UnityEngine; using System.Collections; public class SpriteObjectNumber : MonoBehaviour { public int spObNum; public void Start() { } }
###補足情報(言語/FW/ツール等のバージョンなど)
C#です。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/10/11 03:42
2016/10/11 03:51
2016/10/11 04:01
2016/10/11 04:07
2016/10/11 04:16
2016/10/11 04:24 編集
2016/10/11 04:48
2016/10/11 05:16 編集
2016/10/11 06:10
2016/10/11 06:15
2016/10/11 06:46
2016/10/11 06:52
2016/10/11 07:00
2016/10/11 07:02
2016/10/11 07:06
2016/10/11 07:27
2016/10/11 07:30