게임의 상태에 따라 기능이 달라지는 버튼이 있습니다. 관리자의 버튼에는 OnClick내 게임의 현재 상태를 나타내는 enum을 사용하는 switch 문이 해당 스위치를 반영하도록 업데이트되지 않습니다.
public void Button1(){
Debug.Log ("Pressed");
switch (GameState) {
case States.aState:
Debug.Log ("In A");
Debug.Log (GameState.ToString());
//Do stuff
break;
case States.bState:
Debug.Log("In B");
Debug.Log (GameState.ToString());
//Do stuff
break;
}
} 또한
, 그대로이 복사 내 의심, 나는 또한 다음과 같은 업데이트 기능을 가지고 테스트 (:)) (다음 코드를의 이상과 기능 버전을 포함하는 함수에서 지적 시간)
void Update(){
Debug.Log (GameState.ToString());
}
업데이트 기능은 관리자가 보여주는 것과 게임 상태가 변경되는 것과 일치합니다. 그러나 스위치는 이것을 무시하고 관리자 또는 업데이트 디버그가 보여주는 것과 상관없이 항상 첫 번째 경우로갑니다.
해당 스크립트의 DontDestroyOnLoad() 명령에는이 스위치의 GameState 변수를 포함하여 해당 장면을 운반하는 데 필요한 데이터가 들어 있기 때문에이 스크립트에도 관련 명령이 있습니다.
이 작업을 수행하는 이유에 대한 도움이 감사하겠습니다.
편집 : 명확성을위한 전체 스크립트입니다.
public class StateManager : MonoBehaviour {
#region Variables
/// <summary>
/// States, referring to the individual scenes of the game. Please refer the the "Outline" Word doc or the individual scene scripts in the "Assets/Scripts/RoomCode" folder for details on each state.
/// </summary>
public enum States {aState=0,bState=1,cState=2,dState=3,eState=4,fState=5,gState=6,hState=7,iState=8,jState=9};
public States GameState = States.aState;
/// <summary>
/// Holds a reference to the current scene's Scenemanager.
/// </summary>
public GameObject curSceneManager;
/// <summary>
/// Holds references to the actual files for scenes, assigned in Inspector.
/// </summary>
public Object[] scenes;
/// <summary>
/// Holds whatever scene is currently open.
/// </summary>
public Scene curScene;
/// <summary>
/// Tracks how many items the player has gathered that would summon Phafnir.
/// </summary>
public int PhafnirScore=0;
/// <summary>
/// Tracks how many items the player has gathered that would summon Baphomet.
/// </summary>
public int BaphometScore=0;
#endregion
/// <summary>
/// Set object references. Done in awake so that all objects are fully loaded.
/// </summary>
void Awake(){
//Assign curScene
curScene = SceneManager.GetActiveScene();
//Assign curSceneManager
curSceneManager = GameObject.FindGameObjectWithTag("SceneManager");
//Assign Delegate Subscription to OnSceneLoad, which will cause that function to be called whenever a new scene is loaded
SceneManager.sceneLoaded+=OnSceneLoad;
//set SceneManager to stay loaded between scenes
DontDestroyOnLoad(this.gameObject);
}
void Update(){
Debug.Log (GameState.ToString());
}
void OnSceneLoad(Scene scene, LoadSceneMode mode){
//Assign curScene
curScene = SceneManager.GetActiveScene();
//Assign curSceneManager. Not actually used because of the strange way it conflicts with DontDestroyonload
curSceneManager = GameObject.FindGameObjectWithTag("SceneManager");
}
/// <summary>
/// The two choice button objects are set to call the following two functions,
/// </summary>
public void Button1(){
Debug.Log ("Pressed");
switch (GameState) {
case States.aState:
Debug.Log ("In A");
Debug.Log (GameState.ToString());
if (GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneParent>().ScriptPlace == 3) {
curSceneManager.GetComponent<SceneParent>().ScriptPlace = 4;
}
else if (GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneParent>().ScriptPlace == 5) {
curSceneManager.GetComponent<SceneParent>().ScriptPlace = 6;
curSceneManager.GetComponent<SceneParent>().choice = SceneParent.choices.button1;
}
break;
case States.bState:
Debug.Log("In B");
Debug.Log (GameState.ToString());
GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneParent>().choice = SceneParent.choices.button1;
GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneParent>().ScriptPlace++;
break;
case States.cState:
Debug.Log("In C");
break;
case States.dState:
Debug.Log("In D");
break;
case States.eState:
Debug.Log("In E");
break;
case States.fState:
Debug.Log("In F");
break;
case States.gState:
Debug.Log("In G");
break;
case States.hState:
Debug.Log("In H");
break;
case States.iState:
Debug.Log("In I");
break;
case States.jState:
Debug.Log("In J");
break;
default:
Debug.Log ("Error, result for Button1 not found");
break;
}
}
public void Button2(){
switch (GameState) {
case States.aState:
break;
case States.bState:
GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneParent>().choice = SceneParent.choices.button2;
GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneParent>().ScriptPlace++;
break;
case States.cState:
break;
case States.dState:
break;
case States.eState:
break;
case States.fState:
break;
case States.gState:
break;
case States.hState:
break;
case States.iState:
break;
case States.jState:
break;
default:
Debug.Log ("Error, result for Button2 not found");
break;
}
}
public void Button3(){
switch (GameState) {
case States.aState:
if (GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneParent>().ScriptPlace == 3) {
GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneParent>().ScriptPlace = 5;
}
else if (GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneParent>().ScriptPlace == 5) {
GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneParent>().ScriptPlace = 6;
GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneParent>().choice = SceneParent.choices.button3;
}
break;
case States.bState:
GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneParent>().choice = SceneParent.choices.button3;
GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneParent>().ScriptPlace++;
break;
case States.cState:
break;
case States.dState:
break;
case States.eState:
break;
case States.fState:
break;
case States.gState:
break;
case States.hState:
break;
case States.iState:
break;
case States.jState:
break;
default:
Debug.Log ("Error, result for Button3 not found");
break;
}
}
//The next button, when clicked, simply advances to the next part of the room's script
public void Nextbutton(){
Debug.Log ("OH GOD THEY CLICKED NEXT");
GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneParent>().ScriptPlace++;
}
/// <summary>
/// Called by the RoomManager scripts to change scenes as needed.
/// </summary>
public void loadScene(int s){
//Update state by typecasting
GameState = (States)s;
//S represent the scene's load order in the build settings. a=0 up to j=9, in order;
SceneManager.LoadScene (s);
}
}
양쪽 스 니펫의'GameState'가 같은 변수를 참조하고 다른 스코프에 있지 않다는 것을 확신하십니까? –
전적으로 확신합니다. 그것들은 동일한 스크립트에서 내 코드에서 차례로 이어집니다. 업데이트 함수는 button1 함수가 끝나는 곳의 바로 아래에 위치합니다. – Quelandoris
두 국가 만 있으면'if..else' 문으로 코드를 다시 작성하고 모든 것이 작동하는지 알려주십시오 –