質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

831閲覧

Unity UnityException: GetInt is not allowed to be called from a MonoBehaviour constructor

ssssggg

総合スコア40

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2019/05/12 02:16

編集2019/05/12 04:38

コード

//略 using UnityEngine.UI; using UnityEngine.SceneManagement; public class MapManager : MonoBehaviour { public GameObject Player; public static int EnemyNUM; public static float PXP; public static float PYP; Player PlayerS; float PX; float PY; public GameObject Mmap; public Text messagewindow; messageinthemap MSGM; int flee = 0; public int CMN; public int WorldNUM; int m_syori = 0; public int Z; float Wait = 0.02f; string daihon; string[] MapS1 = { ""}; string[] MapS2 = { ""}; string[] MapS3 = { "0" }; public GameObject ALLBSA; public GameObject BSA; //略 public GameObject BSAZ; void start() { Debug.Log("void"); MSGM = Mmap.GetComponent<messageinthemap>(); PlayerS = Player.GetComponent<Player>(); PlayerPrefs.GetString("PlayerName"); PX =PlayerPrefs.GetFloat("PositionX"); PY =PlayerPrefs.GetFloat("PositionY"); Player.transform.position = new Vector2(PX, PY); messagewindow.text = ""; BSA.SetActive(false); //略 BSAZ.SetActive(false); } IEnumerator OutputMessage(string msg) { flee = 1; MSGM.Hyouzi(); transform.position = new Vector2(-0.8f, -5.5f); for (int i = 0; i < msg.Length; i++) { var outputText = msg.Substring(i, 1); messagewindow.text = messagewindow.text + outputText; yield return new WaitForSeconds(Wait); } m_syori = 0; while (Z == 0) { yield return new WaitForSeconds(0.01f); } messagewindow.text = ""; flee = 0; transform.position = new Vector2(1000, 1000); } void Update() { if (Input.GetKey(KeyCode.Z)) { Z = 1; } } public void BattleStartS() { StartCoroutine("BattleStarts"); } public void Event1() { BSA.SetActive(false); } public void CMM() { StartCoroutine("CMMS"); } IEnumerator CMMS() { daihon = MapS1[CMN];//daihonrist[messageenzan]; MSGSA(); while (Z == 1) { yield return new WaitForSeconds(0.01f); } if (MapS2[CMN] != "0") { daihon = MapS2[CMN];//daihonrist[messageenzan]; MSGSA(); while (Z == 1) { yield return new WaitForSeconds(0.01f); } if (MapS3[CMN] != "0") { daihon = MapS3[CMN];//daihonrist[messageenzan]; MSGSA(); while (Z == 1) { yield return new WaitForSeconds(0.01f); } MSGM.HIHyouzi(); } else { MSGM.HIHyouzi(); } } else { MSGM.HIHyouzi(); } } IEnumerator BattleStart() { WorldNUM = PlayerS.WorldNumber; if (WorldNUM == 1) { EnemyNUM = Random.Range(96, 100); } StartCoroutine("BattleStartAnimation"); flee = 2; while (flee == 0) { yield return new WaitForSeconds(0.01f); } PXP = Player.transform.position.x; PYP = Player.transform.position.y; SceneManager.LoadScene("Kyanon Battle Scene"); } IEnumerator BattleStartAnimation() { float X = Player.transform.position.x; float Y = Player.transform.position.y; ALLBSA.transform.position = new Vector2(X-11.7f,Y+6.3f); yield return new WaitForSeconds(0.02f); BSA.SetActive(true); //省略 BSAZ.SetActive(true); yield return new WaitForSeconds(0.3f);      BSA.SetActive(false); //略 BSAZ.SetActive(false); flee = 0; } IEnumerator Event1S() { } void MSGSA() { messagewindow.text = "";//textを初期化 StartCoroutine("OutputMessage", daihon); m_syori = 0; } void SAVE() { PlayerPrefs.SetFloat("PositionX", Player.transform.position.x); PlayerPrefs.SetFloat("PositionY", Player.transform.position.y); } }

関連コード1

//略 public class Player : MonoBehaviour { Animator animator; public GameObject MapManagger; MapManager script; float movespeed; public string C; //PlayerPrefs.SetInt("Data", 10); public int S = 0; public int BRS; public int WorldNumber; public int BattleS; // Start is called before the first frame update void Start() { this.animator = GetComponent<Animator>(); script = MapManagger.GetComponent<MapManager>(); transform.position = new Vector2(29, -7); } // Update is called once per frame void Update() { transform.rotation = Quaternion.Euler(0, 0, 0); if (S == 0) { if ((Input.GetKey(KeyCode.UpArrow)) || (Input.GetKey(KeyCode.DownArrow)) || (Input.GetKey(KeyCode.RightArrow)) || (Input.GetKey(KeyCode.LeftArrow))) { movespeed = 0.125f; this.animator.speed = (movespeed / 0.1f); } else { movespeed = 0; this.animator.speed = 0; } } else { movespeed = 0; this.animator.speed = 0; } if (Input.GetKey(KeyCode.UpArrow)) { animator.SetInteger("PL", 1); transform.Translate(new Vector2(0, movespeed)); } else { if (Input.GetKey(KeyCode.DownArrow)) { animator.SetInteger("PL", 2); transform.Translate(new Vector2(0, 0 - movespeed)); } else { if (Input.GetKey(KeyCode.RightArrow)) { animator.SetInteger("PL", 3); transform.Translate(new Vector2(movespeed, 0)); } else { if (Input.GetKey(KeyCode.LeftArrow)) { animator.SetInteger("PL", 4); transform.Translate(new Vector2(0 - movespeed, 0)); } } } } if (Input.GetKeyDown(KeyCode.UpArrow)) { animator.SetInteger("PL", 1); } else { if (Input.GetKeyDown(KeyCode.DownArrow)) { animator.SetInteger("PL", 2); } else { if (Input.GetKeyDown(KeyCode.RightArrow)) { animator.SetInteger("PL", 3); } else { if (Input.GetKeyDown(KeyCode.LeftArrow)) { animator.SetInteger("PL", 4); } } } } } // } void OnCollisionEnter2D(Collision2D collision) { Debug.Log("OnCollisionEnter2D: " + collision.gameObject.name); C = collision.gameObject.name; if(C == "Trigger1") { Warp1(); } if (C == "Trigger2") { script.Event1(); } if (C == "Map1") { } } void OnCollisionStay2D(Collision2D collision) { C = collision.gameObject.name; if(C == "Map1") { WorldNumber = 1; } if (Input.GetKeyDown(KeyCode.Z)) { if (C == "Map1") { script.CMN = 1; } script.CMM(); Debug.Log("OnCollisionStay2D"); } } void OnTriggerEnter2D(Collider2D collision) { Debug.Log("OnTriggerEnter2D: " + collision.gameObject.name); C = collision.gameObject.name; if (C == "Trigger2") { int PS = PlayerPrefs.GetInt("Trigger2"); if (PS != 1) { script.Event1(); PlayerPrefs.SetInt("Trigger2",1); } } } void Warp1() { transform.position = new Vector2(-22, -43f); } void Battle() { if (BattleS == 0) { BRS = Random.Range(1, 200); if (BRS >= 180) { script.BattleStartS(); } } } }

関連コード2

//略 public class messageinthemap : MonoBehaviour { float X; float Y; public GameObject player; Player playerS; // Use this for initialization void Start() { transform.position = new Vector2(100, 0); playerS = player.GetComponent<Player>(); } // Update is called once per frame void Update() { } public void Hyouzi() { playerS.S = 1; X = player.transform.position.x; Y = player.transform.position.y; transform.position = new Vector2(X, Y - 5); } public void HIHyouzi() { playerS.S = 0; transform.position = new Vector2(100, 0); } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

vo3

2019/05/12 03:03

ざっとしか見てないので関係あるかは知りませんが、57行目は void Start() ではなく void start() であってますか?
fiveHundred

2019/05/12 03:55

「PlayerPrefs.GetInt()」でエラーが出ていますが、それがどこにもありません。 また、「コンストラクターでPlayerPrefs.GetInt()を呼んではいけない」というエラーなので、スクリプトの生成や呼び出し方法が正しくない可能性もあります。 それらの箇所も記載してください。
ssssggg

2019/05/12 04:13

fiveHundredさん 指摘ありがとうございました。すみません。スクリプトの生成や呼び出し方法とは具体的に何を記載すればよろしいでしょうか?とりあえず、わかることは記載しておきます。 vo3さん 指摘ありがとうございました。Startが小文字になっていました。修正するとDebug.Logが出るようになりました。
ssssggg

2019/05/12 04:39

文字数制限でエラー文が載せられなかったのでここに載せます UnassignedReferenceException: The variable Mmap of MapManager has not been assigned. You probably need to assign the Mmap variable of the MapManager script in the inspector. UnityEngine.GameObject.GetComponent[messageinthemap] () (at /Users/builduser/buildslave/unity/build/Runtime/Export/GameObject.bindings.cs:28) MapManager.Start () (at Assets/MapManager.cs:60)
ssssggg

2019/05/12 04:40

文字数制限でエラー文が載せられなかったのでここに載せます unityexception: GetInt is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'MapManager' on game object 'Map manager'. See "Script Serialization" page in the Unity Manual for further details. UnityEngine.PlayerPrefs.GetInt (System.String key) (at
fiveHundred

2019/05/12 05:36 編集

> スクリプトの生成や呼び出し方法 見た範囲では、特に問題なさそうです。 時々、「MonoBehaviourを継承したクラスをnewで生成する」とかやっている人がいるので、聞いてみただけです。 > UnassignedReferenceException Mmapに何も設定されていない、というエラーです。 インスペクター上でMmapを設定するか、事前にスクリプトでMmapを代入した後に処理を行ってください。 > unityexception at 以降が知りたいので、それを記載してください。
guest

回答1

0

自己解決

Mmapに代入した後自力で何とか解決できました。

投稿2019/05/19 04:49

ssssggg

総合スコア40

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問