
前提
https://gamedevacademy.org/create-a-fast-paced-math-game-in-unity-part-1/
Unityの勉強の一環としてコチラのサイトを参考にゲームを制作しています。
実現したいこと
このサイト通りに作るとリトライができないのでリトライ機能を実装したいです。
発生している問題・エラーメッセージ
Win() または Lose() でテキストを表示した後に SwitchScene() でシーンをリロードしたいのですが、シーンの読み込みがうまくいかず、フリーズしたみたいになってしまいます。
該当のソースコード
C#
1using System.Net.Mime; 2using System.Diagnostics.Contracts; 3using System.Collections; 4using System.Collections.Generic; 5using UnityEngine; 6using UnityEngine.SceneManagement; 7 8public class GameManager : MonoBehaviour 9{ 10 public Problem[] problems; 11 public int curProblem; 12 public float timePerProblem; 13 14 public float remainingTime; 15 16 public PlayerController Player; 17 18 public static GameManager instance; 19 20 void SwitchScene() 21 { 22 SceneManager.LoadScene (SceneManager.GetActiveScene().name); 23 } 24 25 void Awake() 26 { 27 instance = this; 28 } 29 30 void Win() 31 { 32 Time.timeScale = 0.0f; 33 UI.instance.SetEndText(true); 34 SwitchScene(); 35 } 36 37 void Lose() 38 { 39 Time.timeScale = 0.0f; 40 UI.instance.SetEndText(false); 41 SwitchScene(); 42 } 43 44 void SetProblem(int problem) 45 { 46 curProblem = problem; 47 remainingTime = timePerProblem; 48 UI.instance.SetProblemText(problems[curProblem]); 49 } 50 51 void CorrectAnswer() 52 { 53 if(problems.Length - 1 == curProblem) 54 { 55 Win(); 56 }else 57 { 58 SetProblem(curProblem + 1); 59 } 60 } 61 62 void IncorrectAnswer() 63 { 64 Player.Stun(); 65 } 66 67 public void OnPlayerEnterTube(int tube) 68 { 69 if(tube == problems[curProblem].correctTube) 70 { 71 CorrectAnswer(); 72 }else 73 { 74 IncorrectAnswer(); 75 } 76 } 77 78 void Update() 79 { 80 remainingTime -= Time.deltaTime; 81 82 if(remainingTime <= 0.0f) 83 { 84 Lose(); 85 } 86 } 87 88 void Start() 89 { 90 SetProblem(0); 91 } 92} 93
試したこと
タイトル画面を作成してシーンの遷移を行ったところ、遷移自体は成功しました。2回目にシーンをロードしたときにうまくいきませんでした。
補足情報
Unity 2021.3.2f1
回答2件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2022/08/13 03:35
2022/08/13 07:25 編集
2022/08/13 08:32
2022/08/15 03:39 編集
2022/08/15 05:25