[Unity C#] 条件付きでApplication.Quitを行う方法

実現したいこと

Unityで複数シーンを重複なしでランダムに移動したのち、最後のシーンでボタンを押すとゲームが終了する仕組みを作りたいです。

前提

被験者実験用のアプリを開発している大学生です。
表示順による影響を避けるため、ボタン操作によりランダムにシーンを提示できるシステムを作っています。最後のシーンでは同一ボタンでゲームが終了するように設定したいです。
最初にすべてのシーンインデックスを「currentUnvisitedSceneIndexes」に格納しています。シーンを移動するごとに「〃」が減っていき、0になったときにゲームが終了する仕組みです。

発生している問題

ButtonClicked()メソッドにおいて、最終シーンでボタンを押してもゲームが終了しません。

public void ButtonClicked() { // 未訪問のシーンがない場合、ゲームを終了する if (currentUnvisitedSceneIndexes.Count == 0) { Application.Quit(); //ゲームプレイ終了 return; } // ランダムに次のシーンのインデックスを決定 int randomIndex = Random.Range(0, currentUnvisitedSceneIndexes.Count); nextSceneIndex = currentUnvisitedSceneIndexes[randomIndex]; // 決定したシーンのインデックスを未訪問リストから削除 currentUnvisitedSceneIndexes.Remove(nextSceneIndex); // 次のシーンに移動 SceneManager.LoadScene(nextSceneIndex); // 現在のシーンが次のシーンに移動した場合、訪問済みリストに追加 if (nextSceneIndex != currentSceneIndex) { visitedSceneIndexes.Add(currentSceneIndex); } Debug.Log("Updated currentUnvisitedSceneIndexes: " + string.Join(", ", currentUnvisitedSceneIndexes)); Debug.Log("Next scene index: " + nextSceneIndex); }

全体のソースコード

c#

1using UnityEngine;2using UnityEngine.SceneManagement;3using System.Collections.Generic;4using System.Linq;5 6public class Scene : MonoBehaviour 7{8 public static List<int> visitedSceneIndexes = new List<int>();9 public static List<int> currentUnvisitedSceneIndexes = new List<int>();10 public static int currentSceneIndex;11 public static int nextSceneIndex;12 13 public GameObject confirmationPanel; 14 15 public void Start()//ゲーム開始時に呼ばれる16 {17 // 最初のシーンのビルドインデックスは018 if (SceneManager.GetActiveScene().buildIndex == 0)19 {20 // すべてのシーンを未訪問に設定21 int sceneCount = SceneManager.sceneCountInBuildSettings;22 currentSceneIndex = SceneManager.GetActiveScene().buildIndex;23 24 for (int i = 0; i < sceneCount; i++)25 {26 if (i != currentSceneIndex)27 {28 currentUnvisitedSceneIndexes.Add(i);29 }30 }31 32 Debug.Log("Initial currentUnvisitedSceneIndexes: " + string.Join(", ", currentUnvisitedSceneIndexes));33 Debug.Log("Initial visitedSceneIndexes: " + string.Join(", ", visitedSceneIndexes));34 }35 }36 37 public void ButtonClicked()38 {39 // 未訪問のシーンがない場合、ゲームを終了する40 if (currentUnvisitedSceneIndexes.Count == 0)41 {42 Application.Quit(); //ゲームプレイ終了43 return;44 }45 46 // ランダムに次のシーンのインデックスを決定47 int randomIndex = Random.Range(0, currentUnvisitedSceneIndexes.Count);48 nextSceneIndex = currentUnvisitedSceneIndexes[randomIndex];49 50 // 決定したシーンのインデックスを未訪問リストから削除51 currentUnvisitedSceneIndexes.Remove(nextSceneIndex);52 53 // 次のシーンに移動54 SceneManager.LoadScene(nextSceneIndex);55 56 // 現在のシーンが次のシーンに移動した場合、訪問済みリストに追加57 if (nextSceneIndex != currentSceneIndex)58 {59 visitedSceneIndexes.Add(currentSceneIndex);60 }61 62 Debug.Log("Updated currentUnvisitedSceneIndexes: " + string.Join(", ", currentUnvisitedSceneIndexes));63 Debug.Log("Next scene index: " + nextSceneIndex);64 }65 66 private void OnDestroy()67 {68 // 最終シーンでゲームが終了する場合、最終シーンを訪問済みリストに追加69 if (nextSceneIndex == currentSceneIndex)70 {71 visitedSceneIndexes.Add(currentSceneIndex);72 }73 }74}75

試したこと

・該当スクリプトの順番をかえる

エラーコードが出ないので、何を直していいかがわからないです…。

補足情報

必要であれば書き足します、ご不明な点があればお気軽にお願いします。

コメントを投稿

0 コメント